Hilmi
Hilmi

Reputation: 3441

Entity framework: Relation Error

I was trying to optimize the performance for my site so i followed these rules while doing the point about Generating views on compile time i had to create .edmx file, after creating this file and follow all the steps I'm facing this issue:

Schema specified is not valid. Errors: 
The property for the relationship 'FK_dbo_X_dbo_Y_x_id' contains a Role 'X' has a type 'Site.Models.X' that is not valid for a relationship End. Change the End Role to an EntityType.

for every relation i have.

can anyone tell me how can i fix this error?

Update :

How i define my relations

in model :

    [ForeignKey("foreign_id")]
    public EntityCollection<MyClass> relation_obj { get; set; }

in DbContext:

modelBuilder.Entity<X>().HasMany(m => m.relation_obj );

The relation assembly :

[assembly: EdmRelationshipAttribute("DBModel", "FK_dbo_X_dbo_Y_x_id", "X", System.Data.Metadata.Edm.RelationshipMultiplicity.One, typeof(Site.Models.X), "Y", System.Data.Metadata.Edm.RelationshipMultiplicity.Many, typeof(Site.Models.Y), true)]

Upvotes: 1

Views: 768

Answers (1)

Kirill Bestemyanov
Kirill Bestemyanov

Reputation: 11964

Your problem is that you define as navigation properties classes that has not mapping to database (your own defined classes). Don't do it. Entity framework cannot work with such navigation properties. You can read more about navigation properties here.

Problem is here:

[assembly: EdmRelationshipAttribute("DBModel", "FK_dbo_X_dbo_Y_x_id", "X", System.Data.Metadata.Edm.RelationshipMultiplicity.One, typeof(Site.Models.X), "Y", System.Data.Metadata.Edm.RelationshipMultiplicity.Many, typeof(Site.Models.Y), true)]

From MSDN:

"

public EdmRelationshipAttribute(
    string relationshipNamespaceName,
    string relationshipName,
    string role1Name,
    RelationshipMultiplicity role1Multiplicity,
    Type role1Type,
    string role2Name,
    RelationshipMultiplicity role2Multiplicity,
    Type role2Type
)

Parameters

relationshipNamespaceName Type: System.String The name of the namespace for the association in which this entity participates.

relationshipName Type: System.String The name of a relationship in which this entity participates.

role1Name Type: System.String Name of the role for the type at one end of the association.

role1Multiplicity Type: System.Data.Metadata.Edm.RelationshipMultiplicity A value of RelationshipMultiplicity that indicates the multiplicity at one end of the association, such as one or many.

role1Type Type: System.Type The type of the entity at one end of the association.

role2Name Type: System.String Name of the role for the type at the other end of the association.

role2Multiplicity Type: System.Data.Metadata.Edm.RelationshipMultiplicity A value of RelationshipMultiplicity that indicates the multiplicity at the other end of the association, such as one or many.

role2Type Type: System.Type The type of the entity at the other end of the association. "

So role1Type and role2Type should be entities. Not your own defined classes.

Upvotes: 1

Related Questions