2016-07-03 6 views
-1

をdbに他のオブジェクトを追加するときにユーザーを作成しようとします。Entity Frameworkのは、私がアドレスを作成しようとしていると私はそうのような奇妙な例外を取得

は、列にNULL値を挿入することはできません「LoweredUserName」、テーブル'SkiShop.dbo.aspnet_Users';列はNULLを許可しません。 INSERTは失敗します。 ステートメントが終了しました。

私はOKと思ったが、おそらくそこに作成された浮遊ユーザーだがcomittedないが、いや、作成したアドレスを追加しようとする前に、db.saveChanges()コールは罰金行きます!ここでは(ASP.NET MVC 2を使用して)私のコードだ

var userId = Helpers.CreateAspNetUserFromUser(employee.aspnet_Users, employee.TempPass); 
try { 
    db.SaveChanges(); //no probs 
} 
catch (Exception) { 

    throw; // doesn't land here 
} 


if (userId != null) { 
    employee.AspNetUserId = (Guid)userId; 
    try 
    { 
     employee.TempPass = null; 
     Address address = employee.Address; 
     address.CreatedById = employee.CreatedById; 
     address.ModifiedById = employee.CreatedById; 
     address.DateCreated = employee.DateCreated; 
     address.DateModified = employee.DateModified; 

     db.Addresses.AddObject(address); 
     db.SaveChanges(); //throws exception here 

なぜそれが再びユーザーを作成しようとしていますか?アドレスの外部キーがすべてokで、Addressのすべてのプロパティもすべてokであり、データベースの仕様と一致するため、誤ったエラーメッセージの場合は混乱します。

編集:あなたは、データベースの設計にfk_employee_userを持っているので

CREATE TABLE [Address] (
    AddressId int primary key identity(1,1) not null, 
    Street nvarchar(128) not null, 
    HouseNr nvarchar(8) not null, 
    PostCode nvarchar(128) null, 
    [State] nvarchar(128) not null, 
    Country nvarchar(128) not null, 
    Telephone nvarchar(128) not null, 

    --Relationships & auditing 
    CreatedById uniqueidentifier not null, 
    ModifiedById uniqueidentifier not null, 
    DateCreated datetime not null, 
    DateModified datetime not null, 

    Constraint fk_address_cb Foreign Key (CreatedById) References Aspnet_Users(UserId), 
    Constraint fk_address_mb Foreign Key (ModifiedById) References Aspnet_Users(UserId), 
); 

CREATE TABLE Employee (
    SvnNr nvarchar(16) primary key not null, 
    DOB date not null, 

    --Relationships & auditing 
    AddressId int not null, 
    AspNetUserId uniqueidentifier not null, 
    CreatedById uniqueidentifier not null, 
    ModifiedById uniqueidentifier not null, 
    DateCreated datetime not null, 
    DateModified datetime not null, 
    TempPass nvarchar(max) null, 

    Constraint fk_employee_address Foreign Key (AddressId) References [Address](AddressId), 
    Constraint fk_employee_user Foreign Key (AspNetUserId) References Aspnet_Users(UserId), 
    Constraint fk_employee_cb Foreign Key (CreatedById) References Aspnet_Users(UserId), 
    Constraint fk_employee_mb Foreign Key (ModifiedById) References Aspnet_Users(UserId), 
); 
+0

'Address'と' Employee'クラスの定義を参照すると役に立ちます。 – qbik

+0

@qbik - 私は最初にdbで作業していますので、テーブルの作成ステートメントを作成します – rory

答えて

1

Why is it trying to create a user again?とEFはそれを反映している:ここでは、テーブル・ステートメントを作成します。 Employeeをインスタンス化すると、.NetはEmployeeのヌルユーザーオブジェクトも作成します。これらの作成されたオブジェクトは、オブジェクトのインスタントのみであり、そのほとんどのプロパティはnullです。それを回避するために、あなたのAddressEmployeeを保存する前に、

Employee.User = nullのような何かを行います。

この場合、EFはユーザーをEmployerに保存しようとしません。 Employeeオブジェクトに必要なuserIdがあることを確認してください。

+0

Ugh、私はcreatedByとmodifiedBy aspnet_users :-(を追加していました – rory

関連する問題