kmp*_*kmp 7 c# navigation-properties ef-code-first sql-server-2012 entity-framework-4.3.1
我正在使用针对SQL Server 2012数据库的DbContext POCO方法实体框架4.3.1.
我在数据库中只有两个表,它们看起来像这样:

注意:根本没有在数据库中指定外键 - 我只是在模型中强制执行关系(我无法更改数据库).
它们每个都有一行数据,如下所示:

我执行了以下查询以确保连接可以工作:

现在,我有以下实体:
public class Two
{
public long TwoId { get; set; }
public string OneId { get; set; }
public virtual One One { get; set; }
}
public class One
{
public string OneId { get; set; }
public DateTime DeliveryDate { get; set; }
public virtual ICollection<Two> Twos { get; private set; }
public void AddTwo(Two two)
{
if (two == null)
throw new ArgumentNullException("two");
if (Twos == null)
Twos = new List<Two>();
if (!Twos.Contains(two))
Twos.Add(two);
two.One = this;
}
}
Run Code Online (Sandbox Code Playgroud)
这是背景:
public class TestContext : DbContext
{
public TestContext(string conectionString)
: base(conectionString)
{
Configuration.LazyLoadingEnabled = true;
Ones = Set<One>();
Twos = Set<Two>();
}
public DbSet<One> Ones { get; private set; }
public DbSet<Two> Twos { get; private set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
var one = modelBuilder.Entity<One>();
one.ToTable("One");
one.HasKey(d => d.OneId);
var two = modelBuilder.Entity<Two>();
two.ToTable("Two");
two.HasKey(d => new
{
d.OneId,
d.TwoId
});
two.Property(p => p.TwoId)
.HasDatabaseGeneratedOption(DatabaseGeneratedOption.None);
two.HasRequired(t => t.One)
.WithMany(o => o.Twos)
.HasForeignKey(o => o.OneId);
base.OnModelCreating(modelBuilder);
}
}
Run Code Online (Sandbox Code Playgroud)
当我运行这段代码时,我会Why is this printed?打印到我的控制台 - 我没想到,因为你可以看到导航属性应该被填充(我甚至明确地包含它):
using (var ctx = new TestContext(@"......"))
{
const string oneId = "111348718";
var one = ctx.Ones.Single(o => o.OneId.Equals(oneId));
if (one != null)
{
var sdi = ctx
.Twos
.Include(s => s.One)
.Single(s => s.OneId.Equals(oneId));
if (sdi.One == null)
{
Console.WriteLine("Why is this printed?");
}
else
{
Console.WriteLine("This is what I expect");
}
}
}
Run Code Online (Sandbox Code Playgroud)
现在,这真是奇怪的一点:如果我只是DeliveryDate从One类中注释掉它的属性就可以了(我This is what I expect打印到控制台).
这里有什么问题,我该如何解决?
注意:如果我查看变量上的DeliveryDate属性,one它已被正确设置为预期值,因此数据库中的日期必须正常,实体框架完全能够实现它.
更多信息: 它是一个约会的事实并不重要 - 如果是,比如nvarchar它仍然失败 - 似乎任何简单的属性都会导致整个事情崩溃 - 这对我来说就像一个错误......
好的,我已经了解了这一点,我认为这是实体框架中的一个错误,但我可以解决它。这就是它是什么......
Two 表中的 OneId 列末尾有一个空格。
是的 - 想想看 - 这有多可怕!
因此,我编写了一个 sql 脚本,该脚本LTRIM(RTRIM(对数据库中的每个 nvarchar 列执行操作,并且一切正常。
现在,鉴于 SQL Server 不关心空格(如问题中带有联接的查询所证明的那样),我不太喜欢实体框架确实关心空格,特别是因为它在这里完全奇怪且不一致。Single当然,我调用以填充变量的点sdi应该抛出,或者应该填充导航属性 - 一个或另一个,但这种行为真的很令人困惑(在我看来) - 某些任意属性与什么有关鱼价??
为了完整起见,这里简单地重现了这一点。创建一个新数据库,然后在其上运行以下 SQL 脚本:
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[One](
[OneId] [nvarchar](10) NOT NULL,
[SomeInt] [int] NOT NULL,
CONSTRAINT [PK_Delivery] PRIMARY KEY CLUSTERED
(
[OneId] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
CREATE TABLE [dbo].[Two](
[TwoId] [int] NOT NULL,
[OneId] [nvarchar](10) NOT NULL,
CONSTRAINT [PK_DeliveryItem] PRIMARY KEY CLUSTERED
(
[TwoId] ASC,
[OneId] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
INSERT INTO Two(TwoId, OneId) VALUES (1, '1 ')
INSERT INTO One(OneId, SomeInt) VALUES ('1', 1.0)
Run Code Online (Sandbox Code Playgroud)
现在创建 ac# 控制台应用程序,添加对EntityFramework(版本 4.3.1)和System.Data.Entity程序集的引用,放入此代码并运行它 - 它将打印SHOULD NOT BE PRINTED!!!。
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Data.Entity;
using System.Linq;
namespace EFTest
{
public class Two
{
public int TwoId { get; set; }
public string OneId { get; set; }
public virtual One One { get; set; }
}
public class One
{
public string OneId { get; set; }
public virtual ICollection<Two> Twos { get; private set; }
// Comment out this property and it will work
public int SomeInt { get; set; }
public void AddTwo(Two two)
{
if (two == null)
throw new ArgumentNullException("two");
if (Twos == null)
Twos = new List<Two>();
if (!Twos.Contains(two))
Twos.Add(two);
two.One = this;
}
}
public class Context : DbContext
{
public Context(string connectionString)
: base(connectionString)
{
Configuration.LazyLoadingEnabled = true;
Ones = Set<One>();
Twos = Set<Two>();
}
public DbSet<One> Ones { get; private set; }
public DbSet<Two> Twos { get; private set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder
.Entity<One>()
.HasKey(d => d.OneId)
.ToTable("One");
var two = modelBuilder.Entity<Two>();
two.ToTable("Two");
two.HasKey(d => new
{
d.OneId,
d.TwoId
});
two.Property(d => d.TwoId)
.HasDatabaseGeneratedOption(DatabaseGeneratedOption.None);
two.HasRequired(m => m.One)
.WithMany(t => t.Twos)
.HasForeignKey(d => d.OneId);
base.OnModelCreating(modelBuilder);
}
}
internal class Program
{
private static void Main()
{
using (var ctx = new Context(@"your connection string"))
{
const string oneId = "1";
var one = ctx.Ones.Single(o => o.OneId.Equals(oneId));
if (one == null)
{
Console.WriteLine("No row with one ID in the database");
return;
}
var two = ctx
.Twos
.Include(s => s.One)
.Single(s => s.OneId.Equals(oneId));
Console.WriteLine(two.One == null
? "SHOULD NOT BE PRINTED!!!"
: "SHOULD BE PRINTED");
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
然后执行以下操作之一:
SomeInt上的属性OneUPDATE Two SET OneId = RTRIM(OneId))。 两者都可以(显然,修剪是现实生活中唯一合理的解决办法)。
| 归档时间: |
|
| 查看次数: |
131 次 |
| 最近记录: |