我有一个与另一个表有关系的类。
public class MyClass
{
[Key]
public Guid Id {get; set; }
public virtual OtherClass OtherClass { get; set; }
}我把它连接到一个控制器上,并为CRUD创建视图--一切都很好。
在DB中创建了一个OtherClass_OtherClassId列,但该列不在模型中。
如何在控制器的Create方法中将引用放入此Id列?
如何才能强制要求此关系,而不必每次都创建一个全新的OtherClass?
发布于 2012-03-03 02:58:40
带注释的类和一些描述:
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; }
}那么哪个更好呢?Independent associations还是声明外键?
独立关联与对象编程的匹配更紧密。使用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,因为实体验证或attach a stubbed entity (assuming you know the id)需要它。如果显式声明了外键,那么就不会遇到这种情况。
现在使用外键,您会遇到一个不同的问题:
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.总而言之-
独立关联
外键
中的数据库问题
发布于 2012-03-03 01:16:25
EF通常需要与模型配置进行握手。这应该可以让你开始学习了。然而,先做一个关于EF代码和DB的好教程会有很大好处。
以下是has:
通过保留identity OrderTypeId和实际的OrderType ref对象,生成具有多个User OrderType
公共类订单{ public Order() { OrderItems =新OrderItemCollection();}
public int OrderID { get;set;} public int OrderDate { get;set;} public string OrderName { get;set;} public int UserId { get;set;} public虚拟用户OrderUser { get;set;} public virtual OrderItemCollection OrderItems { get;set;} public int?OrderTypeId { get;set;}公共OrderType OrderType { get;set;}公共重写int GetHashCode() { return OrderID.GetHashCode();}
}公共类OrderConfiguration : EntityTypeConfiguration { public OrderConfiguration() { this.ToTable("ORDERS");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)()(X)(True);
this.HasRequired(u => u.OrderUser).WithMany().HasForeignKey(u => u.UserId);this.Property(x => this.HasOptional(u => u.OrderType).WithMany().HasForeignKey(u => u.OrderTypeId);}
}公共类OrderContext : DbContext { protected override void OnModelCreating(DbModelBuilder modelBuilder) {modelBuilder.Configurations.Add(DbModelBuilder OrderConfiguration());}} '
https://stackoverflow.com/questions/9536982
复制相似问题