none
Circular dependencies problem with EF 4.2 Code-First RRS feed

  • Question

  • Hello,

    first of all sorry for my bad English. Now let's explain my problem :).

    I have a model like this:

    <Table("Users")> <Serializable()> <XmlRoot("Users")>
    Public Class Customer
    
    property CustomerID as Integer
    
    property CompleteName as String
    
    property SelectedCustomer as Customer <----
    
    End Class


    When I create a new customer object and I add it to my datacontext, calling db.SaveChanges throw this error:

    Unable to determine a valid ordering for dependent operations.
    Dependencies may exist due to foreign key constraints, model requirements or store generated values.

    This  always happens , even if SelectedCustomer is nothing or not.

    Hovewer, code-first 4.2, generate a correct database in sqlserver (I think so), with a Table named "Users" witch
    contains a nullable ForeignKey SelectedCustomer_CustomerID pointing to the same Users Table


    Where is my error? How can I tell Code-First how to handle this (I think so simple) model?

    Wednesday, July 4, 2012 5:44 PM

Answers

  • Hi Paolo T,

    Welcome to MSDN Forum.

    I'm not sure whether this is caused by EF4.2. I have tested in EF4.3.1, the code works well. Below is my test code.

    class Program
        {
            static void Main(string[] args)
            {
                using (Context context = new Context())
                {
                    Person p1 = context.personSet.Find(1);
                    Person p2 = new Person();
                    p2.Name = "test";
                    p2.p = p1;
                    context.personSet.Add(p2);
                    context.SaveChanges();
                }
            }
        }
    
        class Person
        {
            public int id { get; set; }
            public string Name { get; set; }
            public Person p { get; set; }
        }
    
        class Context : DbContext
        {
            public DbSet<Person> personSet { get; set; }
        }

    I suggest you to upgrade your EF.

    Best Regards


    Allen Li [MSFT]
    MSDN Community Support | Feedback to us

    Friday, July 6, 2012 5:30 AM
    Moderator

All replies

  • Hi Paolo T,

    Welcome to MSDN Forum.

    I'm not sure whether this is caused by EF4.2. I have tested in EF4.3.1, the code works well. Below is my test code.

    class Program
        {
            static void Main(string[] args)
            {
                using (Context context = new Context())
                {
                    Person p1 = context.personSet.Find(1);
                    Person p2 = new Person();
                    p2.Name = "test";
                    p2.p = p1;
                    context.personSet.Add(p2);
                    context.SaveChanges();
                }
            }
        }
    
        class Person
        {
            public int id { get; set; }
            public string Name { get; set; }
            public Person p { get; set; }
        }
    
        class Context : DbContext
        {
            public DbSet<Person> personSet { get; set; }
        }

    I suggest you to upgrade your EF.

    Best Regards


    Allen Li [MSFT]
    MSDN Community Support | Feedback to us

    Friday, July 6, 2012 5:30 AM
    Moderator
  • Hello Allen Li, thank you for your reply.

    I was using EF 4.3.1, but seem it need fulltrust .net environment. My service hosting  instead works in medium trust.

    So when I ran my application with EF 4.3.1, the server thrown a security exception. So I downgraded EF to 4.2 and now it works well.

    If there is a way for run EF 4.3.1 in medium trust, I will upgrade it immediately but meantime I can't. Any solution?

    Regarding the code you wrote:

    the exception is thrown when I create a new person from scratch and p is nothing (or not....) like that:

    static void Main(string[] args)
            {
                using (Context context = new Context())
                {
                    Person p1 = new Person();
                    p1.name="test";
                    p1.p=null;
                    context.personSet.Add(p1);
                    context.SaveChanges();
                }
            }

    or like that:

    static void Main(string[] args)
            {
                using (Context context = new Context())
                {
                    Person p1 = new Person();
                    p1.name="test";
                    Person p2= new Person();
                    p2.name="ciao"
                    p1.p=p2;
                    context.personSet.Add(p1);
                    context.SaveChanges();
                }
            }

    Friday, July 6, 2012 5:52 PM
  • Hi Paolo,

    I'm not sure whether this is an issue in EF4.2, but in EF4.3.1, it works well.

    What the service are you using? WCF or Ado.net data service? Based on this issue, I suggest you to post in related Service Forum, some experts there may help you to find a way to solve the security exeption in EF4.3.1.

    WCF Forum

    WCF Data Services

    Best Regards


    Allen Li [MSFT]
    MSDN Community Support | Feedback to us

    Monday, July 9, 2012 6:26 AM
    Moderator