Unable to determine the main end of the association between types - Entity Framework error, class relation between the same class

Ask for the class below and try to find records in the database, returns an error. Using C #, MVC 4, Entity Framework 4, and SQL Server 2012 Database.

Error

Unable to determine the principal end of an association between the types 'FlexApp.Models.Model.Usuario' and 'FlexApp.Models.Model.Usuario'. The principal end of this association must be explicitly configured using either the relationship fluent API or data annotations. 

Class

 public class Usuario { [Key] public int UsuarioID { get; set; } public string Nome { get; set; } public int UsuCad { get; set; } public int UsuAlt { get; set; } [ForeignKey("UsuCad")] public virtual Usuario UsuarioCad { get; set; } [ForeignKey("UsuAlt")] public virtual Usuario UsuarioAlt { get; set; } } 

FK Database

 alter table USUARIO add constraint USUARIO_fk01 foreign KEY(UsuCad) REFERENCES USUARIO(UsuarioID); alter table USUARIO add constraint USUARIO_fk02 foreign KEY(UsuAlt) REFERENCES USUARIO(UsuarioID); 
+6
source share
3 answers

Answer

After much searching, I found feedback on using InverseProperty Then the code is as follows. This is a ForeignKey property ForeignKey To mount the link and the InverseProperty property to InverseProperty that this field depends on this other camp, change the foreign key.

thanks for the help

 public class Usuario { [Key] public int UsuarioID { get; set; } public string Nome { get; set; } public int UsuCad { get; set; } public int UsuAlt { get; set; } [ForeignKey("UsuCad")] [InverseProperty("UsuarioID")] public virtual Usuario UsuarioCad { get; set; } [ForeignKey("UsuAlt")] [InverseProperty("UsuarioID")] public virtual Usuario UsuarioAlt { get; set; } } 
+3
source

I am not 100% sure which db scheme you are trying to achieve here. My guess is that you want to have a foreign key in the same table. In your comment, you said you want a one to one ship relationship. Technically, this is not possible. Because when you try to insert the first record, you need to specify a value for the foreign key column. The value must be the identifier of an existing record. Wait ... We have no entries yet!

Since 1-1 is not very practical here, you should do one to zero / one. This means that your foreign key column must be NULL so that you can insert the first record with a NULL value in the foreign key column.

It’s a little difficult for me to understand your model names and properties. Therefore, I am going to use a common class / table that everyone can understand, but with your specific requirement (foreign key of an external link)

I do not use data annotations, I use the Fluent API

The stupid business requirement / assumption is this:

  • A person may or may not have a Parent
  • A person may or may not have a kid

So, our entity class will look like this

 public class Person { public int Id { set; get; } public string Name { set; get; } public virtual Person Parent { set; get; } public virtual Person Kid { set; get; } } 

Now, to use the free relationship management API, we need to go to our DBContext class and override the OnModelCreating method

 public class MyDbContext : DbContext { public MyDbContext() : base("MyConnectionStringName") { } protected override void OnModelCreating(DbModelBuilder modelBuilder) { modelBuilder.Entity<Person>().HasKey(d => d.Id) .HasOptional(m => m.Kid).WithOptionalPrincipal(d=>d.Parent); base.OnModelCreating(modelBuilder); } public DbSet<Person> Persons { set; get; } } 

This will create a table with three columns, ID , Name and Parent_Id (NULLABLE, Foreign Key to ID the same table ratio)

I can insert data like this

 var db = new MyDbContext(); var myMom = new Person {Name = "Indira"}; var me = new Person {Name = "Shyju", Parent = myMom}; var myDaughter = new Person { Name = "Gauri", Parent = me}; db.Persons.Add(myMom); db.Persons.Add(me); db.Persons.Add(myDaughter); db.SaveChanges(); 

And you will have the data with the ParentId column with the foreign key in the ID column of the same table.

enter image description here

+2
source

The error is that the Usuario class Usuario exactly the same as in the public virtual Usuario UsuarioAlt { get; set; } public virtual Usuario UsuarioAlt { get; set; }

You cannot use the same type as the navigation property. The problem here is that the navigation properties are incorrect. Think of a SQL database table. Can the same unique primary key be a foreign key in the same table? You need your foreign key to come from another table.

Navigation properties are what the Entity Framework uses to determine the relational model of an object between your POCOs and therefore your database tables.

You need a different class for the relationship. If you have a second class with that name, use namespace aliases, so the runtime might know the difference between the two classes, now it looks like a class that references itself.

You should write your model below

 public class Usuario { [Key] public int UsuarioID { get; set; } public string Name { get; set; } public int UsuCad { get; set; } public int UsuAlt { get; set; } public virtual SomeSecondClass SomeSecondClass { get; set; } public virtual SomeThirdClass SomeThirdClass { get; set; } } public class SomeSecondClass { public int SomeSecondClassID { get; set;} // More properties } public class SomeThirdClass { public int SomeThirdClassID { get; set;} // More properties } 
-1
source

All Articles