Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/entity-framework/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Entity framework 实体框架4.3.1如何创建关联_Entity Framework_Code First - Fatal编程技术网

Entity framework 实体框架4.3.1如何创建关联

Entity framework 实体框架4.3.1如何创建关联,entity-framework,code-first,Entity Framework,Code First,我的代码如下所示 public class User { public int ID { get; set; } public int BillingAddressID { get; set; } public Address BillingAddress { get; set; } public IList<Shipment> Shipments { get; set; } } public class Address { public i

我的代码如下所示

public class User
{
    public int ID { get; set; }
    public int BillingAddressID { get; set; }
    public Address BillingAddress { get; set; }
    public IList<Shipment> Shipments { get; set; }
}

public class Address
{
    public int ID { get; set; }
    public string Street { get; set; }
    public string City { get; set; }
    public string ZipCode { get; set; }
}

public class Shipment
{
    public int ID { get; set; }
    public string State { get; set; }
    public int DeliveryAddressID { get; set; }
    public Address DeliveryAddress { get; set; }
    public User ShipUser { get; set; }
    //[ForeignKey("ShipUser")]
    public int ShipUserID { get; set; }
    //public int UserId { get; set; }
}

public class TestContext : DbContext
{
    public DbSet<User> Users { get; set; }
    public DbSet<Address> Addresses { get; set; }
    public DbSet<Shipment> Shipments { get; set; }

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        modelBuilder.Entity<Shipment>().HasRequired(u => u.ShipUser)
            .WithMany(d => d.Shipments)
            .HasForeignKey(c => c.ShipUserID)
            .WillCascadeOnDelete(false);
    }
}
公共类用户
{
公共int ID{get;set;}
public int billingaddress{get;set;}
公共广播计费地址{get;set;}
公共IList装运{get;set;}
}
公共课堂演讲
{
公共int ID{get;set;}
公共字符串Street{get;set;}
公共字符串City{get;set;}
公共字符串ZipCode{get;set;}
}
公共舱位装运
{
公共int ID{get;set;}
公共字符串状态{get;set;}
public int DeliveryAddressID{get;set;}
公共地址传递地址{get;set;}
公共用户ShipUser{get;set;}
//[外国钥匙(“船舶用户”)]
public int ShipUserID{get;set;}
//public int UserId{get;set;}
}
公共类TestContext:DbContext
{
公共数据库集用户{get;set;}
公共数据库集地址{get;set;}
公共数据库集{get;set;}
模型创建时受保护的覆盖无效(DbModelBuilder modelBuilder)
{
modelBuilder.Entity().HasRequired(u=>u.ShipUser)
.有许多(d=>d.装运)
.HasForeignKey(c=>c.ShipUserID)
.WillCascadeOnDelete(假);
}
}
  • 如果删除覆盖方法,我将得到一个错误“SqlException:在表“Shippings”上引入外键约束“FK_Shippings\u Users\u ShipUserID”,可能会导致循环或多个级联路径。请在删除时指定“NO ACTION”,或在更新时指定“NO ACTION”,或修改其他外键约束。 无法创建约束。请参阅以前的错误。“
  • 如果我删除Shipping类中的ShippUserId,它会正常工作。当我看到ef创建的表时,我在表Shipping中发现了一个名为Shipping\u UserID的列。我不知道为什么
  • 如果将类indenty键重命名为UserID,它也可以正常工作。
  • 反正我也试过了,但我不知道原因,我需要一些关于EF协会的书

  • 如果您没有为一个关系指定没有cascadeDelete=false的映射,那么如果您有两个到
    user
    from
    shipping
    的关系,它将创建多个级联路径。 按照惯例,你可以使用公共密码

    
    公共用户ShipUser{get;set;}
    public int ShipUserID{get;set;}

  • 按照惯例,它将使用
    ShipUserID
    作为外键

  • 如果删除ShipUserID,Ef需要创建自己的外键以保持关系。这是您的“发货\用户ID”

  • 将类indenty key重命名为UserID
    我不明白你的意思

  • 首先