【发布时间】:2013-07-25 09:40:42
【问题描述】:
型号:
public class User
{
public int Id {get; set;}
public int SomeProperty {get; set;}
public virtual Group Group { get; set; }
}
public class Group {
{
public int Id {get; set;}
// other properties
}
运行这个 linq 查询:
myContext.Users.Where(u => u.SomeProperty = 4);
产生这个 sql 查询:
select
extent1.Id as Id
extent1.SomeProperty as SomeProperty
extent1.Group_Id as Group_Id
from
dbo.Users as extent1
奇怪的是,它决定不像其他属性那样对关联列进行驼峰式大小写。这有什么原因吗?
无论如何,我添加了映射代码来尝试修复它:
var entity = modelBuilder.Entity<User>();
entity.HasRequired( a => a.Group )
.WithRequiredDependent()
.Map( a => a.MapKey( "GroupId" ) );
不幸的是,使用 linq 进行查询会产生以下查询:
select
extent1.Id as Id
extent1.SomeProperty as SomeProperty
extent1.GroupId as GroupId
extent1.Group_Id as Group_Id
from
dbo.Users as extent1
它看起来好一点,但显然仍然无法正常工作,因为我的表有列 GroupId 而不是 Group_Id。有谁知道这里发生了什么或如何解决它?
【问题讨论】:
-
是
User - Group1:1?听起来像 n-1。 -
一个组有很多用户。一个用户只能属于一个组。
标签: c# entity-framework-4