实体框架中的外键 [英] Foreign keys in Entity Framework

查看:193
本文介绍了实体框架中的外键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个插入数据库的方法。

我有1:n关系表 Point (n)和(1)。 Point 有外键 idLine



然而,在实体框架中的类 Point 没有 idLine 属性



现在,在我的方法中,我有一个类型为 Point 的对象作为参数,在这里我遇到一个问题,因为我不知道要分配<$

对象的c $ c> idSection
p>如何在 Point 类中添加 idLine 属性

EF4.0 ,因为这在以前的版本中是不可能的( EF1.0 不包括模型中的foreigh key属性)。

在你的生成向导中,你需要确保在模型中包含外键列复选框在生成数据模型时被选中。生成数据时,外键列将被省略。

如果我记得正确,则不能简单地将键赋给已经生成的类。你需要手动编辑所有的映射(不是不可能的,如果你是EF新手,可能会有点麻烦)。或者,您可以简单地从设计区域中删除此类并再次添加(确保此时选中相应的复选框)。如果您没有自定义生成的类,这可能会更简单。


I have a method for inserting in database.

I have 1:n relationship between the tables Point (n) and Line (1). Point has foreign key idLine.

However, the class Point in Entity Framework doesn't have the idLine property.

Now, in my method I have object of type Point, as parameter and here I have a problem because I don't know to what to assign the idSection of the new Point object which is inserted in the table.

How to add the idLine property in the Point class?

解决方案

I'll assume you're using EF4.0, since this is not possible in previous version (EF1.0 does not include foreigh key properties in model).

In your generation wizard you need to make sure that "Include foreign key columns in the model" checkbox is checked when you're generating data model. Otherwise foreign key columns will be omitted when generating data.

If I'm remembering correctly you cannot simply add forign keys to classes that are already generated. You would need to manually edit all the mappings (not impossible, might be little bit troublesome if you are new to EF). Or you can simply remove this class from design area and add it again (making sure appropriate checkbox is checked this time). This might be simpler if you haven't customized generated classes.

这篇关于实体框架中的外键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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