EF Code First 和双向导航属性中的一对一映射
我的任务是将旧应用程序移植到 MVC 3,并希望首先使用 EF 的代码来复制其现有的数据库架构。当前的代码库中充斥着硬编码的 SQL 命令,因此我提出的模型必须与当前系统的期望“兼容”。我知道我可以使用 EF 的 Database First 来执行此操作,但现有架构非常简单,我认为没有理由不使用代码优先,因为我希望在迁移旧的硬编码数据库时能够建立坚实的基础互动。
我需要 POCO 的外观:
public class Owner
{
[Key]
public Guid Owner_Id { get; set; }
// Properties
public string ContactName { get; set; }
public string PhoneNumber { get; set; }
public string Email { get; set; }
// Navigational properties
public virtual ICollection<Plant> Plants { get; set; }
}
public class Plant
{
[Key]
public Guid Plant_Id { get; set; }
// Properties
public string Address { get; set; }
public string City { get; set; }
public string State { get; set; }
public string Zip { get; set; }
// Foreign Keys
public Guid Owner_Id { get; set; }
// Navigational properties
public virtual Owner Owner { get; set; }
public virtual License License { get; set; }
}
public class License
{
[Key] public Guid License_Id { get; set; }
// Properties
public int Total { get; set; }
public int Users { get; set; }
public string Key { get; set; }
// Foreign Keys
public Guid Plant_Id { get; set; }
// Navigational properties
public virtual Plant Plant { get; set; }
}
在尝试创建上下文时出现此错误:
“无法确定类型‘许可证’和‘植物’之间关联的主要端。此关联的主要端必须是使用关系流畅 API 或数据注释显式配置。”
我意识到 Plant 应该有一个对 License_Id 的可为空的 FK 引用,并且 License 不应该有 Plant_Id 的 FK。但这些是我已经拿到的牌。我想做的事在 EF 中可行吗?
I have been tasked with porting an old app to MVC 3, and want to use EF's code first to replicate its existing database schema. The current code base is littered with hard coded SQL commands, and so the model I come up with has to be "compatible" with what the current system expects. I know I could use EF's Database First to do this, but the existing schema is so simple that I see no reason not to use code first, as I want a solid foundation to build on as we migrate away from our old hard-coded database interactions.
What I need the POCOs to look like:
public class Owner
{
[Key]
public Guid Owner_Id { get; set; }
// Properties
public string ContactName { get; set; }
public string PhoneNumber { get; set; }
public string Email { get; set; }
// Navigational properties
public virtual ICollection<Plant> Plants { get; set; }
}
public class Plant
{
[Key]
public Guid Plant_Id { get; set; }
// Properties
public string Address { get; set; }
public string City { get; set; }
public string State { get; set; }
public string Zip { get; set; }
// Foreign Keys
public Guid Owner_Id { get; set; }
// Navigational properties
public virtual Owner Owner { get; set; }
public virtual License License { get; set; }
}
public class License
{
[Key] public Guid License_Id { get; set; }
// Properties
public int Total { get; set; }
public int Users { get; set; }
public string Key { get; set; }
// Foreign Keys
public Guid Plant_Id { get; set; }
// Navigational properties
public virtual Plant Plant { get; set; }
}
It's giving me this error on attempting to create the context:
"Unable to determine the principal end of an association between the types 'License' and 'Plant'. The principal end of this association must be explicitly configured using either the relationship fluent API or data annotations."
I realize that Plant should have a nullable FK reference to a License_Id, and that License shouldn't have a FK for Plant_Id. But these are the cards I have been dealt. Is what I'm trying to do possible in EF?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在 fluid-api 中(在您的上下文类中):
唯一的降级是您实际上将 1 定义为 0 或 1。 (可以在代码中处理。但仍然...)有关一对一关系的更多信息,请查看实体框架代码第一个一对一关系
In fluent-api (in your context class):
The only downgrade is that you actually defined one to zero or one. (which can be handled in code. but still...) For more info about One-to-One relationship please check out Entity Framework Code First One to One Relationship
尝试添加许可证
以让它知道这是外键,其他外键也是如此。
try adding in License
to let it know that this is foreign key, same for the other foreign keys.