当向数据库添加其他对象时,实体框架尝试创建用户 [英] Entity Framework tries to create user when adding other object to db

查看:89
本文介绍了当向数据库添加其他对象时,实体框架尝试创建用户的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


无法将值NULL插入列 LoweredUserName',表'SkiShop.dbo.aspnet_Users';列不允许为空。 INSERT失败。
该语句已被终止。


我以为可以,也许有一个流浪的用户被创建,但没有安装,但没有在尝试创建并添加地址之前,请先调用 db.saveChanges()



这是我的代码(使用ASP.NET MVC 2):

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

throw; //不着陆
}


if(userId!= null){
employee.AspNetUserId =(Guid)userId;
try
{
employee.TempPass = null;
地址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(); //在这里抛出异常

为什么要再次创建一个用户?如果这是一个错误的错误消息,我很困惑,因为地址的外键都可以,地址的所有属性也都可以和数据库规格匹配。



编辑:这里是create table语句:

  CREATE TABLE [Address](
AddressId int primary key identity ,1)not null,
Street nvarchar(128)not null,
HouseNr nvarchar(8)not null,
PostCode nvarchar(128)null,
[State] nvarchar 128)not null,
国家nvarchar(128)not null,
电话nvarchar(128)not null,

- 关系和审计
CreatedById uniqueidentifier not null,
ModifiedById uniqueidentifier not null,
DateCreated datetime not null,
DateModified datetime not null,

约束fk_address_cb外键(CreatedById)参考Aspnet_Users(UserId),
约束fk_address_mb外键(ModifiedById)参考Aspnet_Users(User Id),
);

CREATE TABLE员工(
SvnNr nvarchar(16)主键不为null,
DOB日期不为null,

- 关系和审计
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,

约束fk_employee_address外键(AddressId)引用[Address](AddressId),
约束fk_employee_user外键(AspNetUserId)参考Aspnet_Users (UserId),
约束fk_employee_cb外键(CreatedById)参考Aspnet_Users(UserId),
约束fk_employee_mb外键(ModifiedById)参考Aspnet_Users(UserId),
);


解决方案

为什么尝试再次创建一个用户?因为您的数据库设计中有 fk_employee_user ,而EF反映出来。当您实例化 Employee 时,.Net也会为 Employee 创建一个空用户对象。这些创建的对象只是对象的即时,它们的大多数属性都为空。为避免这种情况,在保存地址 Employee 之前,请执行


Employee.User = null;


在这种情况下,EF永远不会保存雇主的用户。确保您的 Employee 对象中有您想要的userId。


I'm trying to create an address and I get a weird exception like so:

Cannot insert the value NULL into column 'LoweredUserName', table 'SkiShop.dbo.aspnet_Users'; column does not allow nulls. INSERT fails. The statement has been terminated.

I thought ok, perhaps there's a stray user having been created but not comitted but nope, a db.saveChanges() call before trying to create and add the address goes fine!

Here's my code (using 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

Why is it trying to create a user again? If it's a false error message I'm confused because the foreign keys for address are all ok, and all the properties for Address are also all ok and match database specs.

Edit: Here are the create table statements:

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),
);

解决方案

Why is it trying to create a user again? Because you have fk_employee_user in your database design and EF reflects it. When you instantiate Employee, .Net creates a null user object for Employee too. These created objects are just instant of objects and their most properties are null. To circumvent of that, before saving your Address and Employee, do something like

Employee.User=null;

In that case EF never try to save user for Employer . Be sure you have your desired userId in your Employee object.

这篇关于当向数据库添加其他对象时,实体框架尝试创建用户的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆