logo
down
shadow

InvalidOperationException during EF6 code-first database initialization (principal end of this association must be expli


InvalidOperationException during EF6 code-first database initialization (principal end of this association must be expli

By : user2955607
Date : November 22 2020, 10:40 AM
Hope this helps It looks like you're trying to set them up as a one-to-one relationship. In which case you'll need to pick one of them to be the primary table of the relationship, and the other to use a FK into it. Because right now you can't use 2 FKs to point to each other, since it confuses Entity Framework.
EDIT: Based on the comment, I've figured out what you need to do. I wrote my code in C#, but I'm sure it's easily translatable to VB.
code :
public int AssignedOptionID { get; set; }

[ForeignKey("AssignedOptionID"), InverseProperty("Aspects")]
public virtual AspectOption AssignedOption { get; set; }

public virtual List<AspectOption> AspectOptions { get; set; } 
[ForeignKey("AspectID"), InverseProperty("AspectOptions")]
public virtual Aspect Aspect { get; set; }

[ForeignKey("ParentID")]
public virtual AspectOption Parent { get; set; }

public virtual List<Aspect> Aspects { get; set; } 
modelBuilder.Entity<Aspect>()
            .HasMany(a => a.AspectOptions)
            .WithRequired(a => a.Aspect)
            .WillCascadeOnDelete(false);


Share : facebook icon twitter icon
Unable to determine the principal end of an association

Unable to determine the principal end of an association


By : 한문수
Date : March 29 2020, 07:55 AM
it helps some times By [InverseProperty("Foo")] you tell EF that Bar.Foo and Foo.Bars are paired properties in a one to many association, so that's clear.
Then there are Foo.MainBar and Bar.OldFoo. EF does not know how these are related. They could be paired in a one-to-one association, they can be independent, i.e. with a "many" multiplicity on the other side. So you have to tell EF.
code :
modelBuilder.Entity<Bar>().HasOptional(f => f.OldFoo).WithMany()
    .HasForeignKey(f => f.OldFooId);
modelBuilder.Entity<Foo>().HasOptional(f => f.MainBar)
    .WithRequired(b => b.OldFoo)
The principal end of this association must be explicitly configured

The principal end of this association must be explicitly configured


By : Paul Maric
Date : March 29 2020, 07:55 AM
this one helps. You need to mark ReportDescription.CrimeReport with the [Required] attribute:
code :
[Table("ReportDescription")]
public class ReportDescription
{
    [Key]
    public int ImageId { get; set; }

    [Required]
    public virtual CrimeReport CrimeReport { get; set; }

}
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
    modelBuilder.Entity<ReportDescription>()
        .HasRequired(x => x.CrimeReport)
        .WithRequiredDependent(x => x.ReportDescription);
}
Unable to determine the principal end of an association - EF Code First

Unable to determine the principal end of an association - EF Code First


By : Steven Wu
Date : March 29 2020, 07:55 AM
fixed the issue. Will look into that further I have a class "User" on which I have added the following fields: , Try to use InverseProperty attribute:
code :
public class User
{
    public int Id { get; set; }
    [Required]
    [MaxLength(50)]
    public string Login { get; set; }
    [Required]
    [MaxLength(50)]
    public string LastName { get; set; }
    [MaxLength(50)]
    public string FirstName { get; set; }

    [Required]
    public int CreatedBy_Id { get; set; }
    [ForeignKey("CreatedBy_Id")]
    public virtual User CreatedBy { get; set; }

    public int? UpdatedBy_Id { get; set; }
    [ForeignKey("UpdatedBy_Id")]
    public virtual User UpdatedBy { get; set; } 

    //add these properties
    [InverseProperty("CreatedBy")]
    public virtual ICollection<User> WereCreated {get; set;}
    [InverseProperty("UpdatedBy")]
    public virtual ICollection<User> WereUpdated {get; set;}
}
The principal end of this association must be explicitly configured in asp.net MVC code first approach for one to many r

The principal end of this association must be explicitly configured in asp.net MVC code first approach for one to many r


By : Tony Hess
Date : March 29 2020, 07:55 AM
seems to work fine Well, first, you have 2 Department properties. You probably want the foreign key to be DepartmentId. Then you need a collection of students for 1 to many.
code :
// No need for a table attribute if it matches the class name
public class Department
{
    [Key]  // If you called this Id or DepartmentId you would not need the attribute
    public int Did { get; set; }
    public int DName { get; set; }
    public virtual ICollection<Student> Students { get; set; }
}

public class Student
{
    [Key] 
    public int id { get; set; }
    public string name { get; set; }
    public int age { get; set; }
    // EF will make this relationship automatically. But if you like you can keep FK attribute.
    [ForeignKey("Department")]
    public int? DepartmentId { get; set; }
    public virtual Department Department { get; set; }
}
Any downfall (or better alternative) to run initialization code (HTTPHandler - URL association) using a static construct

Any downfall (or better alternative) to run initialization code (HTTPHandler - URL association) using a static construct


By : Lopez Carlos
Date : March 29 2020, 07:55 AM
I think the issue was by ths following , I can answer your second question: the static constructor will only run if you somehow interact with CMyClass. In other words, it's run on demand, not eagerly when you e.g. access the DLL.
shadow
Privacy Policy - Terms - Contact Us © ourworld-yourmove.org