Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/fsharp/3.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 Id';首先使用EntityFramework代码时,不会进行更新_Entity Framework 4_Ef Code First_Entity Framework Migrations - Fatal编程技术网

Entity framework 4 Id';首先使用EntityFramework代码时,不会进行更新

Entity framework 4 Id';首先使用EntityFramework代码时,不会进行更新,entity-framework-4,ef-code-first,entity-framework-migrations,Entity Framework 4,Ef Code First,Entity Framework Migrations,在前面的一个问题之后,我仍然首先在努力学习EF代码 我有3个表(在本例中,实际上还有更多),其中1个表使用多个Id来访问其他表 我有两个问题 1:保存到数据库时未设置装运和交付的Id(保留为“0”)。 2:使用DBMigrations时,会为RecordId创建两次索引 .Index(t => t.RecordId), .Index(t => t.RecordId); 代码示例: 记录类别: public class Record { public Record()

在前面的一个问题之后,我仍然首先在努力学习EF代码

我有3个表(在本例中,实际上还有更多),其中1个表使用多个Id来访问其他表

我有两个问题

1:保存到数据库时未设置装运和交付的Id(保留为“0”)。 2:使用DBMigrations时,会为RecordId创建两次索引

.Index(t => t.RecordId),
.Index(t => t.RecordId);
代码示例:

记录类别:

public class Record
{
    public Record()
    {
        Shipping = new Shipping();
        Delivery = new Delivery();
    }

    public int RecordId { get; set; }
    public int ShippingId { get; set; }
    public int DeliveryId { get; set; }

    public virtual Shipping Shipping { get; set; }
    public virtual Delivery Delivery { get; set; }
}
装运类别:

public class Shipping
{
    public int ShippingId { get; set; }
    public string ShippingName { get; set; }

    public virtual Record Record { get; set; }
}
交货类别:

public class Delivery
{
    public int DeliveryId { get; set; }
    public String DeliveryText { get; set; }

    public virtual Record Record { get; set; }
}
背景:

public class Context : DbContext
{
    public DbSet<Record> Records { get; set; }
    public DbSet<Shipping> Shippings { get; set; }
    public DbSet<Delivery> Deliveries { get; set; }

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {

        modelBuilder.Entity<Record>()
            .HasRequired(m => m.Shipping)
            .WithRequiredDependent(x => x.Record)
            .WillCascadeOnDelete(false);

        modelBuilder.Entity<Record>()
            .HasRequired(m => m.Delivery)
            .WithRequiredDependent(x => x.Record)
            .WillCascadeOnDelete(false);

        base.OnModelCreating(modelBuilder);
    }
主程序(第二次尝试)


为了避免额外的索引,不要在记录类中指定键字段。 要获取默认标识行为名称,请输入密钥字段Id

public class Record
{
    public Record()
    {
        Shipping = new Shipping();
        Delivery = new Delivery();
    }

    public int Id { get; set; }
    public virtual Shipping Shipping { get; set; }
    public virtual Delivery Delivery { get; set; }
}
using (Context context = new Context())
      {               
            var model = context.Records.Create();
            model.Shipping = context.Shippings.Create();
            var shipping = model.Shipping;
            shipping.ShippingName = "TestContext";
            model.Delivery = context.Deliveries.Create();
            var delivery = model.Delivery;
            delivery.DeliveryText = "customText";
            context.Entry(model).State = EntityState.Added;
            context.SaveChanges();
      }
public class Record
{
    public Record()
    {
        Shipping = new Shipping();
        Delivery = new Delivery();
    }

    public int Id { get; set; }
    public virtual Shipping Shipping { get; set; }
    public virtual Delivery Delivery { get; set; }
}