Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.1k views
in Technique[技术] by (71.8m points)

entity framework - EF Core second object reference generates circular dependency

Is it not possible to have a second reference to second class? FirstClass contains SecondClasses and SeocondBegin containing the begin element. With this code I get the execption in SaveChanges:

System.InvalidOperationException: 'Unable to save changes because a circular dependency was detected in the data to be saved: 'FirstClass { 'Id': -2147482647 } [Added] <-
SecondClasses FirstClass { 'FirstClassId': -2147482647 } SecondClass { 'Id': -2147482647 } [Added] <-
SecondBegin { 'SecondBeginId': -2147482647 } FirstClass { 'Id': -2147482647 } [Added]'.'

I would like the have this property because the second class should be a 'linked list' and the collection SecondClasses does not containing the The source is:

namespace EFTestApp
{
    public class FirstClass
    {
        public int Id { get; set; }
        public int? SecondBeginId { get; set; }
        public string Name { get; set; }
        [ForeignKey(nameof(SecondBeginId))]
        public SecondClass SecondBegin { get; set; }
        [InverseProperty(nameof(EFTestApp.SecondClass.FirstClass))]
        [IgnoreDataMember]
        public ICollection<SecondClass> SecondClasses { get; set; }
    }
}

namespace EFTestApp
{
    public class SecondClass
    {
        public int Id { get; set; }
        public int FirstClassId { get; set; }
        public string Url { get; set; }
        [ForeignKey(nameof(FirstClassId))]
        public FirstClass FirstClass { get; set; }
        public SecondClass Next { get; set; }
    }
}

namespace EFTestApp
{
    public class ApplicationDbContext : DbContext
    {
        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        {
            optionsBuilder.EnableSensitiveDataLogging();
            optionsBuilder.UseSqlServer(@"Server=(localdb)MSSQLLocalDB;Database=sample;Trusted_Connection=True");
        }

        public DbSet<FirstClass> FirstClasses { get; set; }
        public DbSet<SecondClass> SecondClasses { get; set; }
    }
}

namespace EFTestApp
{
    class Program
    {
        static void Main(string[] args)
        {
            var dbContext = new ApplicationDbContext();
            dbContext.Database.EnsureCreated();

            var firstClass = new FirstClass()
            {
                Name = "First"
            };
            var secondClass = new SecondClass()
            {
                FirstClass = firstClass,
                Url = "Blablah"
            };
            
            firstClass.SecondBegin = secondClass;

            dbContext.Add(firstClass);
            dbContext.SaveChanges();
        }
    }
}
question from:https://stackoverflow.com/questions/65904581/ef-core-second-object-reference-generates-circular-dependency

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

It's fine to have a cycle, but you can't create it with a single call to SaveChanges() as EF isn't able to INSERT either row without the other.

You'll have to call SaveChanges twice here, once to insert the entities, and then again to "close" the cycle.

Eg

dbContext.Add(firstClass);
dbContext.Add(secondClass);
dbContext.SaveChanges();

firstClass.SecondBegin = secondClass;
dbContext.SaveChanges();

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...