Asp.net mvc 新手EF4.3&;MVC4关系

Asp.net mvc 新手EF4.3&;MVC4关系,asp.net-mvc,entity-framework-4,Asp.net Mvc,Entity Framework 4,我有一个与另一个表有关系的类 public class MyClass { [Key] public Guid Id {get; set; } public virtual OtherClass OtherClass { get; set; } } 我将其连接到一个控制器,并为CRUD创建视图——一切正常 在DB中创建了OtherClass_OtherClassId列,但该列不在模型中 在控制器的Create方法期间,如何将引用放置在此Id列中 如何在每次不必创建全新

我有一个与另一个表有关系的类

public class MyClass
{
    [Key]
    public Guid Id {get; set; }

    public virtual OtherClass OtherClass { get; set; }
}
我将其连接到一个控制器,并为CRUD创建视图——一切正常

在DB中创建了OtherClass_OtherClassId列,但该列不在模型中

在控制器的Create方法期间,如何将引用放置在此Id列中


如何在每次不必创建全新的OtherClass的情况下强制此关系为[必需]?

EF通常需要手持模型配置。这应该让你开始。不过,先学习EF代码和DB代码将非常有益

以下是:

  • 具有多个OrderItems的订单
  • 单用户
  • 以及通过保留标识OrderTypeId和实际OrderType ref对象而生成的单个OrderType

    公共阶级秩序 { 公共秩序() { OrderItems=新的OrderItemCollection(); }

    } 公共类OrderConfiguration:EntityTypeConfiguration { 公共订单配置() { 本表为ToTable(“订单”); this.HasKey(p=>p.OrderID); this.Property(x=>x.OrderID).HasColumnName(“ORDER_ID”); this.Property(x=>x.OrderName).HasMaxLength(200); this.HasMany(x=>x.OrderItems).WithOptional().HasForeignKey(x=>x.OrderID).WillCascadeOnDelete(true)

    } 公共类OrderContext:DbContext { 模型创建时受保护的覆盖无效(DbModelBuilder modelBuilder) { 添加(neworderconfiguration()); } } "


带说明的注释类:

public class MyClass
{
    // [Key] - Don't actually need this attribute
    // EF Code First has a number of conventions.
    // Columns called "Id" are assumed to be the Key.
    public Guid Id {get; set; }

    // This reference creates an 'Independent Association'.  The Database
    // foreign key is created by convention and hidden away in the code.
    [Required]
    public virtual OtherClass OtherClass { get; set; }

    // This setup explicitly declares the foreign key property.
    // Again, by convention, EF assumes that "FooId" will be the key for
    // a reference to object "Foo"

    // This will still be required and a cascade-on-delete property
    // like above - an int? would make the association optional.
    public int OtherClass2Id { get; set; }

    // Leave the navigation property as this - no [Required]
    public virtual OtherClass2 { get; set; }
}
那么哪一个更好呢?还是声明外键

独立关联与对象编程的匹配更为紧密。在OOP中,一个对象并不真正关心成员的Id。ORM试图掩盖这些关系,取得了不同程度的成功

声明外键会将数据库问题放入模型中,但在某些情况下,这会使处理EF变得更加容易

示例-当更新具有所需独立关联的对象时,EF希望将整个对象图放置到位

public class MyClass
{
    public int Id {get; set; }
    public string Name { get; set; }
    [Required]  // Note the required.  An optional won't have issues below.
    public virtual OtherClass OtherClass { get; set; }
}

var c = db.MyClasses.Find(1);
c.Name = "Bruce Wayne";

// Validation error on c.OtherClass.
// EF expects required associations to be loaded.
db.SaveChanges();  
如果您只想更新名称,则还必须从数据库中提取OtherClass,因为它是实体验证所必需的,或者。如果显式声明外键,则不会出现这种情况

现在使用外键时,您会遇到另一个问题:

public class MyClass
{
    public Guid Id {get; set; }
    public string Name { get; set; }

    public int OtherClassId { get; set }
    public virtual OtherClass OtherClass { get; set; }
}

var c = db.MyClasses.Find(1);

// Stepping through dubugger, here, c.OtherClassId = old id

c.OtherClass = somethingElse;

// c.OtherClassId = old id - Object and id not synced!

db.SaveChanges();               

// c.OtherClassId = new id, association persists correctly though.
总括而言—

独立协会

  • 好:匹配OOP和POCO更好
  • 坏:通常需要一个完整的对象图,即使您只更新一个或两个属性。更多令人头痛的问题
外键

  • 好:工作起来更容易,有时也会少一些EF头痛
  • 错误:可能与其对象不同步
  • 坏:POCO中的数据库问题

谢谢。我以前先使用EF作为数据库,但我不熟悉先编码。我很惊讶最终的结果会有很大的不同。你不会先遇到数据库的问题。
public class MyClass
{
    public int Id {get; set; }
    public string Name { get; set; }
    [Required]  // Note the required.  An optional won't have issues below.
    public virtual OtherClass OtherClass { get; set; }
}

var c = db.MyClasses.Find(1);
c.Name = "Bruce Wayne";

// Validation error on c.OtherClass.
// EF expects required associations to be loaded.
db.SaveChanges();  
public class MyClass
{
    public Guid Id {get; set; }
    public string Name { get; set; }

    public int OtherClassId { get; set }
    public virtual OtherClass OtherClass { get; set; }
}

var c = db.MyClasses.Find(1);

// Stepping through dubugger, here, c.OtherClassId = old id

c.OtherClass = somethingElse;

// c.OtherClassId = old id - Object and id not synced!

db.SaveChanges();               

// c.OtherClassId = new id, association persists correctly though.