我正在使用EF6,我在所有者和小工具之间有1:N的关系。我似乎无法获得相关的所有者信息。
public partial class Owners
{
public Owners()
{
Widgets = new HashSet<Widgets>();
}
public int Id { get; set; }
public string OwnerName { get; set; }
public virtual ICollection<Widgets> Widgets { get; set; }
}
public partial class Widgets
{
public int id { get; set; }
public int OwnerId { get; set; }
public string WidgetName { get; set; }
public string WidgetType { get; set; }
public virtual Owners Owners { get; set; }
}
public partial class Model1 : DbContext
{
public Model1(): base("name=Model1"){}
public virtual DbSet<Owners> Owners { get; set; }
public virtual DbSet<Widgets> Widgets { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<Owners>()
.HasMany(e => e.Widgets)
.WithRequired(e => e.Owners)
.HasForeignKey(e => e.OwnerId)
.WillCascadeOnDelete(false);
}
}
问题是当我运行以下查询时,我只获取Widgets的信息
对业主来说没什么。如果我将生成的SQL拉出并在数据库中运行它,我将获得所有信息和相关实体信息?
我相信我在这里忽略了一些简单的事情。
using (var db = new Model1())
{
var rslt = db.Widgets.Include(y => y.Owners);
var rslt2 = await rslt.Where(s => s.WidgetType == "Garthug").ToListAsync();
return rslt2;
}
这是EF生成的SQL语句,它创建并显示我在数据库中运行时所寻找的所有正确信息。 (我手动输入但它应该是正确的)
SELECT
Widgets.id AS id,
Widgets.OwnerId AS OwnerId,
Widgets.WidgetName AS WidgetName,
Widgets.WidgetsType AS WidgetType,
Owners.Id AS Id1,
Owners.OwnerName AS OwnerName
FROM [dbo].Widgets AS Widgets
INNER JOIN [dbo].Owners AS Owner ON Widgets.OwnerId = Owners.Id
WHERE Widgets.WidgetType = 'Garthug'
看来除了Asp身份表之外,我的设置对于每个表都是正确的......有谁知道为什么?
答案 0 :(得分:0)
Widgets.OwnerId与Owner.Id相关联,因此类型相同
但是.NET和数据库之间的排序顺序和相等性并不一定相同。因此,您可能在数据库和EF之间存在整理冲突,其中Widget.OwnerId在数据库的排序规则中匹配Owner.Id,但在.NET的字符串比较规则中不匹配。
EG
using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ef62test
{
class Program
{
public partial class Owner
{
public string Id { get; set; }
public string OwnerName { get; set; }
public virtual ICollection<Widget> Widgets { get; } = new HashSet<Widget>();
}
public partial class Widget
{
public int id { get; set; }
public string OwnerId { get; set; }
public string WidgetName { get; set; }
public string WidgetType { get; set; }
public virtual Owner Owners { get; set; }
}
public partial class Model1 : DbContext
{
public virtual DbSet<Owner> Owners { get; set; }
public virtual DbSet<Widget> Widgets { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<Owner>()
.HasMany(e => e.Widgets)
.WithRequired(e => e.Owners)
.HasForeignKey(e => e.OwnerId)
.WillCascadeOnDelete(false);
}
}
static void Main(string[] args)
{
Database.SetInitializer(new DropCreateDatabaseAlways<Model1>());
string ownerId = "owner1";
using (var db = new Model1())
{
db.Database.Log = s => Console.WriteLine(s);
var o = new Owner() { Id = ownerId };
db.Owners.Add(o);
for (int i = 0; i < 10; i++)
{
o.Widgets.Add(new Widget());
}
db.SaveChanges();
ownerId = o.Id;
db.Database.ExecuteSqlCommand("update widgets set ownerId = UPPER(ownerId);");
}
using (var db = new Model1())
{
db.Database.Log = s => Console.WriteLine(s);
db.Configuration.LazyLoadingEnabled = false;
var owner = db.Owners.Include(o => o.Widgets).Where(o => o.Id == ownerId).First();
Console.WriteLine(owner.Widgets.Count());
}
Console.WriteLine("Hit any key to exit.");
Console.ReadKey();
}
}
}
答案 1 :(得分:0)
如果是aspnetusers表,它是否与其他表存在于同一个上下文中?这可以解释为什么即使数据库没有,EF也在苦苦挣扎?