如何首先在 EF 4.1 代码中映射父列
在我的项目中,我有以下 DomainModel。
public class Login
{
public Guid Id { get; set; }
public Login CreatedBy {get; set; }
}
我使用如下流畅的配置:
modelBuilder.Entity<Login>()
.HasKey(x => x.Id)
.ToTable("Login");
modelBuilder.Entity<Login>()
.HasOptional(x => x.CreatedBy)
.WithMany()
.HasForeignKey(x => x.CreatedBy);
我在存储库中获取所有登录数据的代码如下:
return from d in Db.Logins.Include("CreatedBy")
select d;
当我执行代码时,我收到以下错误:
外键组件“CreatedBy”不是类型“上声明的属性”登录'。验证它是否没有从模型中明确排除,并且它是一个有效的原始属性。
任何人都可以建议我在这里做错了什么吗?
提前致谢
In my project I have following DomainModel.
public class Login
{
public Guid Id { get; set; }
public Login CreatedBy {get; set; }
}
I am using fluent configuration as below:
modelBuilder.Entity<Login>()
.HasKey(x => x.Id)
.ToTable("Login");
modelBuilder.Entity<Login>()
.HasOptional(x => x.CreatedBy)
.WithMany()
.HasForeignKey(x => x.CreatedBy);
My code in repository to get all Logins data is as below:
return from d in Db.Logins.Include("CreatedBy")
select d;
When I execute the code I am getting following error:
The foreign key component 'CreatedBy' is not a declared property on type 'Login'. Verify that it has not been explicitly excluded from the model and that it is a valid primitive property.
Can anyone suggest what I am doing wrong here?
Thanks in advance
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的外键映射
.HasForeignKey(x => x.CreatedBy)
不使用原始属性。然后将其映射为
Your foreign key mapping
.HasForeignKey(x => x.CreatedBy)
does not use a primitive property.Then map it like