How can i add tow property in my model in EF code first from one model?

Hamza abu-othman

I want to add two properties from the city model:

after migration this error shows up:

Unable to determine the relationship represented by navigation 'City.Orders' of type 'ICollection'. Either manually configure the relationship, or ignore this property using the '[NotMapped]' attribute or by using 'EntityTypeBuilder.Ignore' in 'OnModelCreating'.

here is my code :

public class Order
{
    public virtual City FromCity { get; set; }
    public virtual City ToCity { get; set; }
}

public class City
{
    public int Id { get; set; }
    public string Name { get; set; }
    public virtual ICollection<Order> Orders { get; set; }
}
Mohi

I suppose your model is more complicated than just FromCity and ToCity because I don't think it's a good idea to store such information in a different table. Yet, You can use inheritance in this case.

The table-per-hierarchy (TPH) pattern is used by default to map the inheritance in EF. TPH stores the data for all types in the hierarchy in a single table.

However, for your scenario, you can have a base class that holds all related attributes.

public class CityBase
{
    public int Id { get; set; }
    public string Name { get; set; } = string.Empty;
}

Then suppose you need two entities as per your scenario:

public class FromCity : CityBase
{
    public virtual ICollection<Order> Orders { get; set; } = null!;
}
public class ToCity : CityBase
{
    public virtual ICollection<Order> Orders { get; set; } = null!;
}

And the order entity:

public class Order
{
    public int Id { get; set; }
    public string OrderTitle { get; set; } = string.Empty;
    public virtual FromCity FromCity { get; set; } = null!;
    public virtual ToCity ToCity { get; set; } = null!;
}

This approach can solve your problem with a One-to-Many relationship between Orders and FromCity, ToCity as per below diagram:

Relationship Diagram

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How can i fill a value from one my domain model to my edit model?

How to add additional tables to an existing code first from database EF model?

How can I create an EF Code First data model for AdventureWorks2014?

How can I list all answers from answer model to my question model in Django ? Relation many to one?

How can I both load my Simulink model to my Arduino and add also my own code with it?

how can i add objects from my model to my CBV in django?

How can I optimize my EF Code First query?

If I review an existing project, how to determine if it is EF Code First, or Model First, or DB First?

Model First: how to add property of type Color?

Can I add the Model function property in the API?

How to add image property in my model?

How to Add values from JSON to Model, In my Code Below

How can I access the attributes from my model in my view

How can we add multiple foreign keys from one model to another on ASP.NET Core & EF Core & C#

With Django, how can I add my model's ForeignKey to CreateView?

How to create two navigation properties to one model (EF6 Code First)

How do I add an additional property to a Model?

How do I pass the primary key from one model to another model and display some fields of the first model in the template?

Can I project the result of my query to the model generated by EF?

How can i make One-to-One relationship between the CinemaWebsiteUser Model which is inheriting from the IdentityUser Model with another Model

How can I create a model property based on specific model fields?

Solutions with one EF6 model (db first) and one code first

How can I make certain properties of my EF code-first classes internal to my DAL?

How can I use insert_id from one method in model in another method in the same model

I want to send object of my model class from activity-one to activity-three directly. How can this will be achieved?

How do I access a property of one particular instance of a model class across all instances of my UITableViewCell class?

EF6 Code first model ForeignKey

How can I add more collections in this model?

How can I model the "popularity" concept in code?