我应该如何声明外键关系,使用code首先实体框架(4.1)在MVC3? [英] How Should I Declare Foreign Key Relationships Using Code First Entity Framework (4.1) in MVC3?

查看:179
本文介绍了我应该如何声明外键关系,使用code首先实体框架(4.1)在MVC3?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在寻找如何通过声明code第一EF 4.1没有多少运气外键关系等方面的限制对资源。基本上我建设code中的数据模型,并使用MVC3来查询该模型。通过一切工作的MVC这是伟大的(荣誉微软!),但现在我想它不工作,因为我需要的数据模型约束。

I have been searching for resources on how to declare foreign key relationships and other constraints using code first EF 4.1 without much luck. Basically I am building the data model in code and using MVC3 to query that model. Everything works via MVC which is great (kudos to Microsoft!) but now I want it NOT to work because I need to have data model constraints.

例如,我有有一吨是外部对象(表)属性的Order对象。现在,我可以创建一个订单没有问题,但没有能够添加外键或外部对象。 MVC3设置这件事没问题。

For example, I have a Order object that has a ton of properties that are external objects (tables). Right now I can create an Order no problem, but without being able to add the foreign key or external objects. MVC3 sets this up no problem.

我意识到,我可以只添加的对象自己在之前的控制器类保存,但我想调用DbContext.SaveChanges()失败,如果约束关系没有得到满足。

I realize that I could just add the objects myself in the controller class prior to save, but I would like the call to DbContext.SaveChanges() to fail if the constraint relationships have not been met.

新信息

所以,准确地说,我想一个
  当我尝试发生异常
  保存订单对象,而不
  指定的客户对象。这个
  似乎并没有成为行为,如果我
  刚刚组成的对象所描述
  在大多数code首先EF文件。

So, specifically, I would like an exception to occur when I attempt to save an Order object without specifying a customer object. This does not seem to be the behavior if I just compose the objects as described in most Code First EF documentation.

最新code:

public class Order
{
    public int Id { get; set; }

    [ForeignKey( "Parent" )]
    public Patient Patient { get; set; }

    [ForeignKey("CertificationPeriod")]
    public CertificationPeriod CertificationPeriod { get; set; }

    [ForeignKey("Agency")]
    public Agency Agency { get; set; }

    [ForeignKey("Diagnosis")]
    public Diagnosis PrimaryDiagnosis { get; set; }

    [ForeignKey("OrderApprovalStatus")]
    public OrderApprovalStatus ApprovalStatus { get; set; }

    [ForeignKey("User")]
    public User User { get; set; }

    [ForeignKey("User")]
    public User Submitter { get; set; }

    public DateTime ApprovalDate { get; set; }
    public DateTime SubmittedDate { get; set; }
    public Boolean IsDeprecated { get; set; }
}

这是错误我现在得到当访问VS生成视图病人:

This is the error I get now when accessing the VS generated view for Patient:

错误信息

这是属性的ForeignKeyAttribute
  在类型'病人'
  'PhysicianPortal.Models.Order'不是
  有效。外键名父
  关于依赖型未发现
  PhysicianPortal.Models.Order。该
  名称值应以逗号分隔
  外键的属性名称列表。

The ForeignKeyAttribute on property 'Patient' on type 'PhysicianPortal.Models.Order' is not valid. The foreign key name 'Parent' was not found on the dependent type 'PhysicianPortal.Models.Order'. The Name value should be a comma separated list of foreign key property names.

问候,

圭多

推荐答案

如果你有一个订单类,添加引用模型中的另一个类的属性,例如客户应该足以让EF知道有在那里的关系:

If you have an Order class, adding a property that references another class in your model, for instance Customer should be enough to let EF know there's a relationship in there:

public class Order
{
    public int ID { get; set; }

    // Some other properties

    // Foreign key to customer
    public virtual Customer Customer { get; set; }
}

您可以随时将 FK 关系明确:

You can always set the FK relation explicitly:

public class Order
{
    public int ID { get; set; }

    // Some other properties

    // Foreign key to customer
    [ForeignKey("Customer")]
    public string CustomerID { get; set; }
    public virtual Customer Customer { get; set; }
}

ForeignKeyAttribute 构造函数的字符串作为参数:如果你将它放在它重新presents关联的导航属性的名称外键属性。如果你把它放在导航属性将其重新presents相关联的外键的名称。

The ForeignKeyAttribute constructor takes a string as a parameter: if you place it on a foreign key property it represents the name of the associated navigation property. If you place it on the navigation property it represents the name of the associated foreign key.

这句话的意思是,如果你在哪里放置 ForeignKeyAttribute 客户属性,该属性将采取客户ID 在构造函数中:

What this means is, if you where to place the ForeignKeyAttribute on the Customer property, the attribute would take CustomerID in the constructor:

public string CustomerID { get; set; }
[ForeignKey("CustomerID")]
public virtual Customer Customer { get; set; }


基于编辑的最新code
你得到,因为这行的错误:


EDIT based on Latest Code You get that error because of this line:

[ForeignKey("Parent")]
public Patient Patient { get; set; }

EF会寻找一个名为来使用它作为外键执法财产。你可以做两件事情:

EF will look for a property called Parent to use it as the Foreign Key enforcer. You can do 2 things:

1)删除 ForeignKeyAttribute 并把它替换了 RequiredAttribute标签标记为需要的关系:

1) Remove the ForeignKeyAttribute and replace it with the RequiredAttribute to mark the relation as required:

[Required]
public virtual Patient Patient { get; set; }

装饰用 RequiredAttribute标签也有一个很好的副作用的属性:在数据库中的关系与创建ON D​​ELETE CASCADE

Decorating a property with the RequiredAttribute also has a nice side effect: The relation in the database is created with ON DELETE CASCADE.

我也建议让物业虚拟启用延迟加载。

I would also recommend making the property virtual to enable Lazy Loading.

2)创建一个名为属性,将作为一个外键。在这种情况下,它可能更有意义叫它例如 PARENTID (你需要在 ForeignKeyAttribute 以及):

2) Create a property called Parent that will serve as a Foreign Key. In that case it probably makes more sense to call it for instance ParentID (you'll need to change the name in the ForeignKeyAttribute as well):

public int ParentID { get; set; }

在我在这种情况下,尽管它工作得更好有它周围的其他方式的经验:

In my experience in this case though it works better to have it the other way around:

[ForeignKey("Patient")]
public int ParentID { get; set; }

public virtual Patient Patient { get; set; }

这篇关于我应该如何声明外键关系,使用code首先实体框架(4.1)在MVC3?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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