0

In in a website's database I am working on, I have two tables for example: Country & CountryLocale. The two tables contain the following columns:

Country: Id, Longitude, Latitude CountryLocale: Id, CountryId, CountryName, CultureId

What I want is:
- When I retrieve a Country, I want the Entity to contain: CountryId, CountryName, CultureId, Latitude & Longitude.
- When I create a new Country, to insert one record into Table Country and one to Table CountryLocale.
- When I create a new Country Locale, to create a record inside CountryLocale only

etc ...

Is this attainable? Thanks

2 Answers 2

1

You can use entity splitting to partially achieve that. CountryLocale would use the PK of Country as its PK. You can not insert to CountryLocale without a Country. Essentially its a single entity split into multiple tables.

    modelBuilder.Entity<Country>()
        .Map(mc =>
        {
            mc.Properties(n => new
            {
                n.Id,
                n.Longitude,
                n.Latitude
            });
            mc.ToTable("Country");
        })
        .Map(mc =>
        {
            mc.Properties(n => new
            {
                n.CountryName,
                n.CultureId
            });
            mc.ToTable("CountryLocale");
        });
Sign up to request clarification or add additional context in comments.

3 Comments

CountryId cannot be PK of CountryLocale, as I might have multiple CountryLocale records per Country. Where would the above code? More illustration please. Thanks
I am not using CodeFirst by the way. I have the Database already present and using Entity Framework.
@bhaidar Then obviously you can not map the properties in to a single entity. You would have to use a one-to-many mapping.
0

use discriminator (Table per Hierarchy)

modelBuilder.Entity<CountryBase>()
    .Map<Country>(c => c.Requires("Type").HasValue((int)CountryType.Country))
    .Map<CountryLocale>(c => c.Requires("Type").HasValue((int)CountryType.CountryLocale));

and you can define your sub-entities as you like

1 Comment

Can i have more illustration please? Thanks

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.