【发布时间】:2011-10-20 18:59:47
【问题描述】:
我的任务是将旧应用程序移植到 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; }
}
尝试创建上下文时出现此错误:
“无法确定类型 'License' 和 'Plant' 之间关联的主体端。必须使用关系流 API 或数据注释显式配置此关联的主体端。”
我意识到 Plant 应该具有对 License_Id 的可为空的 FK 引用,并且该许可证不应该具有 Plant_Id 的 FK。但这些是我收到的牌。我想在 EF 中做的事情可能吗?
【问题讨论】:
标签: c# asp.net-mvc-3 entity-framework ef-code-first