Schema specified is not valid. Errors: The relationship not loaded because the type is not available - entity-framework

I have the entities Dependency, Product and Access. Dependency is connected to Product and Access. When i try to create a object set of Access with:
this.context.CreateObjectSet<Access>();
It's working... but when i try to create a object set of Product i get this error: Schema specified is not valid. Errors: The relationship 'Model.FK_Product_Dependency' was not loaded because the type 'Model.Dependency' is not available.
Any ideas?
OBS: i'm working with Database to Model, and with EF 4.0
POCO Entities:
public class Dependency
{
public virtual int Id { get; set; }
public virtual int IdParent { get; set; }
public virtual string Name { get; set; }
public virtual decimal Type { get; set; }
public virtual Dependency Parent { get; set; }
}
public class Product
{
public virtual int Id { get; set; }
public virtual int IdDependency { get; set; }
public virtual decimal Type { get; set; }
public virtual string Name { get; set; }
public virtual string Obs { get; set; }
public virtual Dependency Dependency { get; set; }
}
public class Access
{
public virtual int Id { get; set; }
public virtual int IdProfile { get; set; }
public virtual string Name { get; set; }
public virtual Profile Profile { get; set; }
public virtual ICollection<Dependency> Dependencies { get; set; }
}

Solved. So, the POCO entities cannot be in different namespaces/dlls if access each other. In the exemple above, Dependency and Access was in a namespace/dll and Product in another. I thought that was only necessary the POCO entity be the same (properties and name) like the entity mapped by EF, but is necessary that the entities are in the same namespace/dll (only to the entities that access each other).

Related

Net Core ApplicationUser with foreign key to itself

I'm using Net Core 2.2 and Entity Framework. I have an ApplicationUser class used for multiple user types with foreign keys to each other but entity framework gives an error when adding a migration:
Unable to determine the relationship represented by navigation property 'ApplicationUser.Class' of type 'Class'. Either manually configure the relationship, or ignore this property using the '[NotMapped]' attribute or by using 'EntityTypeBuilder.Ignore' in 'OnModelCreating'.
I'm unsure what the proper way to implement this is. Can anyone help me?
My ApplicationUser class:
public class ApplicationUser : IdentityUser
{
public string CustomerId { get; set; }
public string TeacherId { get; set; }
public int? ClassId { get; set; }
public int? SettingsId { get; set; }
public virtual ApplicationUser Customer { get; set; }
public virtual ApplicationUser Teacher { get; set; }
public virtual Class Class { get; set; }
public virtual Settings Settings { get; set; }
public virtual ICollection<Class> Classes { get; set; }
public virtual ICollection<License> Licenses { get; set; }
public virtual ICollection<Exercise> Exercises { get; set; }
public virtual ICollection<GameProgress> GameProgresses { get; set; }
public virtual ICollection<StudentExercise> StudentExercises { get; set; }
}
And because it's in the error, my Class class:
public class Class
{
public int Id { get; set; }
public string TeacherId { get; set; }
public int? SettingsId { get; set; }
public virtual ApplicationUser Teacher { get; set; }
public virtual Settings Setting { get; set; }
public virtual ICollection<Exercise> Exercises { get; set; }
public virtual ICollection<ApplicationUser> Students { get; set; }
}
The problem has nothing to do with self-referential ApplicationUser relationships. The exception specifically tells you this is a problem with Class. The issue is that you've got a reference property to Class on ApplicationUser, but on Class, you've got both a collection of ApplicationUsers (Students) and a separate reference to ApplicationUser (Teacher). Simply, EF doesn't know which this foreign key is actually referring to.
The solution is exactly what the exception tells you: add fluent config to clear up the ambiguity:
public override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<ApplicationUser>()
.HasOne(x => x.Class)
.WithMany(x => x.Students);
}

Can't set the Relation between 2 DB Models

I Got This Issue:
I Have the Application User Class Like This
public class ApplicationUser : IdentityUser
{
public ROLES Role { get; set; }
public int? CompanyId { get; set; }
public int? AreaId { get; set; }
public string Document { get; set; }
public bool Enable { get; set; }
[ForeignKey("CompanyId")]
public virtual Company Company { get; set; }
[ForeignKey("AreaId")]
public virtual Area Area { get; set; }
public virtual ICollection Measures { get; set; }
}
And I Got this another Model:
public class Area
{
public int AreaId { get; set; }
public string AreaName { get; set; }
public int CompanyId { get; set; }
public string UserId { get; set; }
[ForeignKey("CompanyId")]
public virtual Company Company { get; set; }
[Key, ForeignKey("UserId")]
public ApplicationUser ApplicationUser { get; set; }
}
And when i try to:
add-migration
the PM Console throws:
Unable to determine the principal end of an association between the types 'x.Models.ApplicationUser' and 'x.Models.Area'. The principal end of this association must be explicitly configured using either the relationship fluent API or data annotations.
I have been trying all day but I can't find a way to tell the Entity Framework to recognize the relation.
Any ideas?
Thanks for reading
Add Attribute for AreaId in Area class
[Key]
public int AreaId { get; set; }
and if you want 1-1 relationship for ApplicationUser and Area update your code like
[Unique]
[ForeignKey("UserId")]
public ApplicationUser ApplicationUser { get; set; }
The principal end of this association must be explicitly configured using either the relationship fluent API or data annotations
This Post give me the Answer I Need!!!
It's pretty hard to find...
So I let you the post here...
Thanks for all of your help!

EF6 Foreign Key Issue

Here are my models
public class Driver
{
public int Id { get; set; }
public String Name { get; set; }
public int VehicleId { get; set; }
public virtual Vehicle Vehicle { get; set; }
}
public class Vehicle
{
public int Id { get; set; }
public String Name { get; set; }
public virtual Driver Driver { get; set; }
public int VehicleGroupId { get; set; }
public virtual VehicleGroup Vehicles { get; set; }
}
I'm getting the following error on updating database:
Unable to determine the principal end of an association between the types 'AppName.Models.Vehicle' and 'AppName.Models.Driver'. The principal end of this association must be explicitly configured using either the relationship fluent API or data annotations.
I wish to solve the issue using data annotations. I've tried putting foreign key attribute over Driver Navigation property in vehicle model. But no success. Any help is much appreciated.
To solve your problem you need to explicitly set the end of the association like this :
public class Vehicle
{
[Key, ForeignKey("Driver")]
public int Id { get; set; }
public String Name { get; set; }
public virtual Driver Driver { get; set; }
public int VehicleGroupId { get; set; }
public virtual VehicleGroup Vehicles { get; set; }
}
I think your model is inccorect because a driver can use many vehicles in real life ;)

Multiplicity not allowed. Entity Framework

I am attempting to use MVC4 for the first time and am receiving the following error when I try to create a controller? Could someone kindly steer me in the right direction?
Microsoft Visual Studio
System.Data.Entity.Edm.EdmAssociationEnd: : Multiplicity is not valid
in Role 'PropertyData_DNISData_Target' in relationship
'PropertyData_DNISData'. Because the Dependent Role properties are not
the key properties, the upper bound of the multiplicity of the
Dependent Role must be '*'.
public class PropertyData
{
[Key]
public virtual string PropertyID { get; set; }
[ForeignKey ("DNISData")]
public virtual string DNIS { get; set; }
public virtual string PropertyName { get; set; }
public virtual string PropertyGreeting { get; set; }
public virtual string PropertyOperator { get; set; }
public virtual string InvalidEntryPrompt { get; set; }
public virtual string NoEntryPrompt { get; set; }
public virtual string Comment { get; set; }
public virtual DNISData DNISData { get; set; }
}
public class DNISData
{
[Key]
public virtual string DNIS { get; set; }
[ForeignKey("PropertyData")]
public string PropertyID { get; set; }
public virtual string VDN { get; set; }
public virtual string PropertyGreeting { get; set; }
public virtual string Comment { get; set; }
public virtual PropertyData PropertyData { get; set; }
}
public class DigitData
{
[ForeignKey ("DNISData")]
[Key]
public virtual string DNIS { get; set; }
[Key]
public virtual string Digit { get; set; }
public virtual string InvalidEntryPrompt { get; set; }
public virtual DNISData DNISData { get; set; }
}
You have a 1 to 1 relationship between PropertyData and DNISData. This can only be done via shared primarykey in EntityFramework.
This question can give you the anwser you are looking for:
How to declare one to one relationship using Entity Framework 4 Code First (POCO)

EF Code-First creates some fields ,even I added ForeignKey annotations

can any one help me in this ?
Here is my 2 classes
class Request
{
public Nullable<int> BuyCurrencyId {get ; set;}
public Nullable<int> SaleCurrencyId {get ; set;}
[ForeignKey("SaleCurrencyId")]
public virtual Currency SaleCurrency { get; set; }
[ForeignKey("BuyCurrencyId")]
public virtual Currency BuyCurrency { get; set; }
}
class Currency
{
public int Id { get; set; }
public string Name { get; set; }
public virtual ICollection<Request> Requests { get; set; }
public virtual ICollection<Request> Requests1 { get; set; }
}
I checked the updated with EF database , and I found out that the EF create Reqyests table like this :
SaleCurrencyId int (Already exists)
BuyCurrencyId int (Already exists)
Currency_Id int (Added by EF)
Currency_Id1 int (Added by EF)
By this not thing I expect. I thing the last tow columns are not correct and they not be exist.
Can any one help me ?
I am using EF 6 alpha to update the existing database with my generated model by T4.Please keep it in mind that I want to use data annotations , not Fluent API
Sorry about my bad English
Update 1 :
I thought if I change the Currency class to this it will resolve my problem , but it did not.
class Currency
{
public int Id { get; set; }
public string Name { get; set; }
[InverseProperty("SaleCurrencyId")]
public virtual ICollection<Request> Requests { get; set; }
[InverseProperty("BuyCurrencyId")]
public virtual ICollection<Request> Requests1 { get; set; }
}
Your Update1 is almost the correct solution, but the parameter of the [InverseProperty] attribute must be the navigation property in Request, not the foreign key property:
[InverseProperty("SaleCurrency")]
public virtual ICollection<Request> Requests { get; set; }
[InverseProperty("BuyCurrency")]
public virtual ICollection<Request> Requests1 { get; set; }