Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/entity-framework/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在同一DataContext中的不同父实体中添加与子实体相同类型的集合_C#_Entity Framework_Dbcontext - Fatal编程技术网

C# 在同一DataContext中的不同父实体中添加与子实体相同类型的集合

C# 在同一DataContext中的不同父实体中添加与子实体相同类型的集合,c#,entity-framework,dbcontext,C#,Entity Framework,Dbcontext,类别集合将是交易实体或产品实体的子级。 而产品集合总是属于子类。但在向产品添加类别集合时,它也会显示事务中添加的类别。 这是代码 private void buttonUpdate_Click(object sender, RoutedEventArgs e) { using (var data = new ProductContext()) { var t = new Transaction();

类别集合将是交易实体或产品实体的子级。 而产品集合总是属于子类。但在向产品添加类别集合时,它也会显示事务中添加的类别。 这是代码

    private void buttonUpdate_Click(object sender, RoutedEventArgs e)
    {
        using (var data = new ProductContext())
        {
            var t = new Transaction();
            data.Transaction.Add(t);
            t.Dscr = "t1";

            var c = new Category{  Name= "cat under trans 1"};
            t.Categories.Add(c);
            var p = new Product{ Name="p 1"};

            var c1 = new Category{  Name= "cat under product 1"};
            p.Categories.Add(c1);

            //******p.Categories ALSO SHOWS t.Categories **************//

            c.Products.Add(p);

            data.SaveChanges(); 
        }
    }

public class ProductContext : DbContext
{
    public ProductContext()
    {
        this.Database.Connection.ConnectionString = "workstation id=.;packet size=4096;user id=sa ;Password =123;data source=.; Initial Catalog=YRX;persist security info=False; MultipleActiveResultSets=True; connection timeout=5"; //ConnectionString                            // ConnectionString;
        //this.Configuration.AutoDetectChangesEnabled = true;

    }
    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
        modelBuilder.Conventions.Remove<IncludeMetadataConvention>();
        //base.OnModelCreating(modelBuilder);
    }
    public DbSet<Transaction> Transaction { get; set; }
}

public class Transaction
{
    public Transaction()
    {
        this.Categories = new ObservableCollection<Category>();
    }
    [Key]
    public int TransId { get; set; }
    public string Dscr { get; set; }
     [ForeignKey("TransId")] 
    public virtual ObservableCollection<Category> Categories { get; private set; }

}

public class Category
{
    public Category()
    {
        this.Products = new ObservableCollection<Product>();
    }
    [Key]
    public int CategoryId { get; set; }
    public string Name { get; set; }
    [ForeignKey("CategoryId")] 
    public virtual ObservableCollection<Product> Products { get; private set; }
    public int? ProductId { get; set; }
    public int? TransId { get; set; }
}
public class Product
{
    public Product()
    {
        this.Categories = new ObservableCollection<Category>();
    }
    [Key]
    public int ProductId { get; set; }
    public string Name { get; set; }
    //public virtual Category Category { get; set; }
    public int CategoryId { get; set; }
    [ForeignKey("ProductId")] 
    public virtual ObservableCollection<Category> Categories { get; private set; }
}
private void按钮更新\单击(对象发送者,路由目标)
{
使用(var data=new ProductContext())
{
var t=新事务();
数据.Transaction.Add(t);
t、 Dscr=“t1”;
var c=新类别{Name=“trans 1下的cat”};
t、 类别.增加(c);
var p=新产品{Name=“p 1”};
var c1=新类别{Name=“产品1下的类别”};
p、 增加(c1);
//******p、 Categories还显示t.Categories**************//
c、 产品.加入(p);;
data.SaveChanges();
}
}
公共类ProductContext:DbContext
{
公共产品上下文()
{
this.Database.Connection.ConnectionString=“工作站id=;数据包大小=4096;用户id=sa;密码=123;数据源=;初始目录=YRX;持久安全信息=False;MultipleActiveResultSets=True;连接超时=5”;//ConnectionString//ConnectionString;
//this.Configuration.AutoDetectChangesEnabled=true;
}
模型创建时受保护的覆盖无效(DbModelBuilder modelBuilder)
{
modelBuilder.Conventions.Remove();
modelBuilder.Conventions.Remove();
//基于模型创建(modelBuilder);
}
公共数据库集事务{get;set;}
}
公共类事务
{
公开交易()
{
this.Categories=新的ObservableCollection();
}
[关键]
公共int TransId{get;set;}
公共字符串Dscr{get;set;}
[外键(“TransId”)]
公共虚拟可观察集合类别{get;private set;}
}
公共类类别
{
公共类别()
{
this.Products=新的ObservableCollection();
}
[关键]
public int CategoryId{get;set;}
公共字符串名称{get;set;}
[外国钥匙(“类别”)]
公共虚拟可观察集合产品{get;private set;}
public int?ProductId{get;set;}
公共int?TransId{get;set;}
}
公共类产品
{
公共产品()
{
this.Categories=新的ObservableCollection();
}
[关键]
public int ProductId{get;set;}
公共字符串名称{get;set;}
//公共虚拟类别{get;set;}
public int CategoryId{get;set;}
[外键(“产品ID”)]
公共虚拟可观察集合类别{get;private set;}
}

那么问题是什么?如何在交易和产品下添加类别,当我在交易下添加类别时,上面的代码也会显示在产品下的类别中