निष्पादित नहीं कर सकता है त्रुटि के समाधान के साथ मुझे कोई समस्या है संग्रह संग्रह संशोधित किया गया था, गणना ऑपरेशन निष्पादित नहीं हो सकता है। ऐसा तब होता है जब "लेखक" और "z" एक ही तत्व का सुझाव देते हैं।इकाई फ्रेमवर्क संग्रह संशोधित किया गया था; गणना ऑपरेशन
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Entity;
namespace ConsoleApplication1
{
public class Nation
{
public int ID { get; set; }
public int name { get; set; }
public virtual ICollection<NationAlly> NationAllys { get; set; }
}
public class NationAlly
{
public int ID { get; set; }
public int level { get; set; }
public Nation Natio { get; set; }
}
public class NationsContext : DbContext
{
public DbSet<Nation> Nations { get; set; }
public DbSet<NationAlly> NationAllys { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<Nation>()
.HasMany(n => n.NationAllys)
.WithRequired()
.Map(conf => conf.MapKey("OwnerID"))
.WillCascadeOnDelete(false);
modelBuilder.Entity<NationAlly>()
.HasRequired(a => a.Natio)
.WithMany()
.Map(conf => conf.MapKey("UserID"))
.WillCascadeOnDelete(false);
}
}
class Program
{
static void Main(string[] args)
{
using (var context = new NationsContext())
{
// We have three Nations and two Allies
Nation nation1 = new Nation()
{
name = 1
};
Nation nation2 = new Nation()
{
name = 2
};
Nation nation3 = new Nation()
{
name = 3
};
context.Nations.Add(nation1);
context.Nations.Add(nation2);
context.Nations.Add(nation3);
context.SaveChanges();
}
using (var context = new NationsContext())
{
Nation z = (from x in context.Nations
where x.name == 1
select x).FirstOrDefault();
Nation author = (from x in context.Nations
where x.name == 1
select x).ToList().FirstOrDefault();
NationAlly ally1 = new NationAlly()
{
Natio = author
};
// toNation of ally1 refers to Nation2
// ally1.User = author;
if (z.NationAllys != null)
{
z.NationAllys.Add(ally1);
}
else
{
z.NationAllys = new List<NationAlly>();
z.NationAllys.Add(ally1);
}
context.SaveChanges();
}
}
}
}
मैं इकाई की रूपरेखा पर कोड का परीक्षण किया 4.1 और 5
हाँ से बचने के लिए आपको कुछ संदर्भों में शून्य असाइन करना चाहिए! धन्यवाद। मेरी समस्या स्वयं-संदर्भ प्रकार भी थी, जहां मैं ईएफ को इंगित करने की कोशिश कर रहा था कि स्पष्ट रूप से दोनों तरीकों का संदर्भ था। बच्चे को हटाने के बाद-> मूल संदर्भ, यह ठीक काम किया! –