我有一个拥有多对多自我关系的实体.作为一个例子考虑这个实体:
public class User
{
public int ID { get; set; }
public string UserName { get; set; }
public virtual ICollection<User> Friends { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
以下是我配置映射的方法:
HasMany(t => t.Friends).WithMany()
.Map(m => {
m.MapLeftKey("UserID");
m.MapRightKey("FriendID");
m.ToTable("UserFriends");
});
Run Code Online (Sandbox Code Playgroud)
由于此关系现在由EF管理,我实际上无法访问UserFriends DbSet我的代码并且无法处理对它的并发访问.为了使这个组合处理并发访问(添加/删除),我是否需要自己处理多对多关系,然后添加一个[Timestamp]列或者有没有办法告诉EF自己同时处理它?就像模型构建器中的配置一样.
编辑:我正在使用EF 6,目前如果实体上有并发操作(例如,尝试删除当前未在数据库上退出的朋友),我收到以下错误消息,并且DbUpdateException:
保存不公开其关系的外键属性的实体时发生错误.EntityEntries属性将返回null,因为无法将单个实体标识为异常的来源.通过在实体类型中公开外键属性,可以更轻松地在保存时处理异常.有关详细信息,请参阅InnerException.
我需要创建一系列列表的笛卡尔积.例如,我有:
{{4,3,7},{1,2,9},{5,8}}
我需要:{4,1,5},{4,1,8},{3,1,5},{3,1,8},......,{7,9,8}
到目前为止,我已经了解到你可以使用这样的东西:
var lists = new List<List<int>>
{
new List<int> { 4, 3, 7},
new List<int> { 1, 2, 9},
new List<int> { 5, 8},
};
IEnumerable<IEnumerable<int>> empty = new[] { Enumerable.Empty<int>() };
var agg = lists.Aggregate(
empty,
(acc, next)
=>
(from ac in acc
from n in next
select ac.Concat(new[] {n})));
Run Code Online (Sandbox Code Playgroud)
但是,最初当我实现这个时,我实现它是这样的:
var lists = new List<List<int>>
{
new List<int> { 4, 3, 7},
new List<int> { 1, 2, 9},
new List<int> { 5, 8},
};
var …Run Code Online (Sandbox Code Playgroud) 目前代码契约不允许派生类中成员的前提条件,其中成员已经在基类中设置了前提条件(我实际上当前得到警告而不是错误).我不明白这背后的逻辑.我理解它与Liskov的替换规则有关,声明派生类应始终能够在父预期的地方使用.当然"使用"意味着按预期工作.对于接口而言,这对我来说似乎没问题,因为实现接口的不同类型不会添加状态,因此可以完全强制合同.但是,当您从基类继承时,您正在这样做以添加状态和特殊功能,并且通常情况下,覆盖方法会有额外的要求.为什么不能像前置条件和对象不变量一样将前置条件与AND组合在一起?
看看下面:
class Speaker
{
public bool IsPlugged { get; set; }
protected virtual void Beep()
{
Contract.Requires(IsPlugged);
Console.WriteLine("Beep");
}
}
class WirelessSpeaker : Speaker
{
public bool TransmitterIsOn { get; set; }
protected override void Beep()
{
Contract.Requires(TransmitterIsOn);
base.Beep();
}
}
Run Code Online (Sandbox Code Playgroud)
你可能会争辩说这个类层次结构打破了Liskov的规则,因为当传递给期望a的方法时,无线扬声器可能无法发出蜂鸣声Speaker.但这不是我们使用代码合同的原因吗?确保满足要求?
.net c# liskov-substitution-principle code-contracts solid-principles
所以我一直在尝试用Roslyn以编程方式编译一段代码.由于某些原因,我添加的引用不会在Compilation类中结束.因此,当我使用'AddReferences'后查看引用的程序集时,列表为空.因此,当我尝试发射时,我在诊断中没有定义"对象".有谁能指出我的问题?
Microsoft.CodeAnalysis.SyntaxTree syntaxTree = CSharpSyntaxTree.ParseText(@"
public static class Program
{
public static void Main()
{
System.Console.WriteLine(""Hello"");
}
}
");
string autoreferences = @"mscorlib.dll,System.Core.dll";
List<string> usings = new List<string>();
string netAssembliesDirectory = Path.GetDirectoryName(typeof(object).Assembly.Location);
var refs = new List<string>();
foreach (string reference in autoreferences.Split(','))
refs.Add(netAssembliesDirectory + "\\" + reference);
CSharpCompilation compilation = CSharpCompilation.Create("ConsoleTest")
.WithOptions(
new CSharpCompilationOptions(OutputKind.ConsoleApplication).WithUsings("System"))
.AddSyntaxTrees(syntaxTree);
compilation.AddReferences(refs.Where(r => r != "").Select(r => MetadataReference.CreateFromFile(r)));
var er = compilation.Emit(@"C:\" + "ConsoleTest");
Run Code Online (Sandbox Code Playgroud) 这是分配给该Text物业的文字(原谅质量差):

我已经直接从TextBox表单上复制文本,将其复制到记事本++并显示所有字符.正如你所看到的,一切都很好.
以下是TextBox表单中的外观:

看起来间距现在都搞砸了.可能是什么问题呢?
编辑:对于MultiLine,ReadOnly和WordWrap,TextBox设置为true.
c# ×5
.net ×1
compilation ×1
generics ×1
linq ×1
liskov-substitution-principle ×1
roslyn ×1
textbox ×1
winforms ×1