我想删除拆分为两个实体的表行。
如果我尝试删除主要实体,那么在不使用context.Entry(...).Reference
.. 加载相关其他实体之前会收到错误消息。
在我要删除整行之前,检索相关实体有点傻吗?
如果我继续评论该context.Entry(...)
行,则会收到以下错误
遇到无效的数据。缺少必需的关系。检查状态条目以确定违反约束的来源。
我在下面添加代码。能否请某人帮助我删除拆分的实体,而不必之前“加载”相关的实体?
using System.Data.Entity;
using System.Linq;
namespace Split
{
class Program
{
static void Main(string[] args)
{
using (var context = new DataContext())
{
var product = new Product()
{
Name = "my Article",
Photo = new ProductPhoto() { PhotoUrl = "http://myfoto.jpg" }
};
context.Products.Add(product);
context.SaveChanges();
}
using (var context = new DataContext())
{
var product = context.Products.First();
//context.Entry(product).Reference(e => e.Photo).Load();
context.Products.Remove(product);
context.SaveChanges();
}
}
}
class Product
{
public virtual …
Run Code Online (Sandbox Code Playgroud) I try do adhere DDD principles on C# collections see more here
And I notice that the model builder method for initial seed HasData relies on the Add Method of ICollection. There is any way to circumvent or trick that method when is called from database update / migrate process?
All I had done until now to trick it follows this path.
1) Create a wrapper around the ICollection named ReadOnlyKollection
2) Have a private ICollection on the model, …