を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),
);
'Address'と' Employee'クラスの定義を参照すると役に立ちます。 – qbik
@qbik - 私は最初にdbで作業していますので、テーブルの作成ステートメントを作成します – rory