Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8162671
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T18:47:55+00:00 2026-06-06T18:47:55+00:00

The following is using EF 5.0.0-rc and Code First. In my design, I have

  • 0

The following is using EF 5.0.0-rc and Code First. In my design, I have an attribute entity:

public class Attribute
{ 
    public int AttributeId { get; set; }
    public Guid Guid { get; set; }
    public string Name { get; set; }
    public string Value { get; set; }

    /* Used for testing the first fluent statement */
    public virtual ICollection<Customer> Customers { get; set; }
}

I also have multiple entities containing a GUID:

public class Customer
{ 
    public int CustomerId { get; set; }
    public Guid Guid { get; set; }
}

public class Location
{ 
    public int LocationId { get; set; }
    public Guid Guid { get; set; }
}

I would like the attribute table to be common to both the customer and location table, with no columns or tables in between. I just can’t seem to get the correct mapping in the fluent API to create a FK without a helper table:

        modelBuilder.Entity<Customer>()
            .HasMany(o => o.Attributes)
            .WithMany(o => o.Customers)
            .Map(m => m.MapLeftKey("Guid"));

… will generate a CustomerAttributes table, which it shouldn’t need.

        modelBuilder.Entity<Organization>()
            .HasMany(o => o.Attributes)
            .WithOptional()
            .HasForeignKey(o => o.Guid);

… won’t compile because

The types of all properties in the Dependent Role of a referential constraint must be the same as the corresponding property types in the Principal Role.

How should the relationship be setup? Or is the design not appropriate?

Edit: Success!

On Raphaël Althaus direction, I was ready to give into EF’s ways and live with separate tracking tables for each entity, but his suggestion to create a new class that the Cust and Loca entities will inherit set me off on the right direction.

First I created a “parent” class, which also gave me a place to refactor some of the audit data that is stored on most entities:

public class ParentEntity
{
    [Key]
    public Guid Guid { get; set; }

    public DateTime? CreatedOn { get; set; }
    public string CreatedBy { get; set; }
    public DateTime? ModifiedOn { get; set; }
    public string ModifiedBy { get; set; }

    [Timestamp]
    public byte[] Version { get; set; }

    public virtual ICollection<Attribute> Attributes { get; set; }
}

Then I inherited the parent class on the Cust and Loca entities:

public class Customer : ParentEntity
{ 
    public int CustomerId { get; set; }
    public Guid Guid { get; set; }
}

public class Location : ParentEntity
{ 
    public int LocationId { get; set; }
    public Guid Guid { get; set; }
}

I also modified the Attribute class to support a new FK field, EntityGuid:

public class Attribute
{
    public int AttributeId { get; set; }
    public Guid EntityGuid { get; set; }
    public string Name { get; set; }
    public string Value { get; set; }
}

Which gave me almost everything I needed, except … it was trying to store every entity in the new ParentEntity table. To fix that I used:

        modelBuilder.Entity<Customer>().ToTable("Customers");
        modelBuilder.Entity<Location>().ToTable("Locations");

And finally the piece that brings it all together:

        modelBuilder.Entity<ParentEntity>()
            .HasMany(e => e.Attributes)
            .WithRequired()
            .HasForeignKey(e => e.EntityGuid);

The only drawback I can tell is the ParentEntity takes over as the primary key, which is a Guid. But I kept my other keys in place though and plan to use those as clustering index.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T18:47:58+00:00Added an answer on June 6, 2026 at 6:47 pm

    Your mixing object world and rDBMS world.

    You’re in an ORM, so you shouldn’t mind (well, not completeley, but in this case, yes) what’s really in your Database.

    A rDBMS can’t manage a many to many relationship without a “link” (relation) table (how to represent a foreign key which is a list ? Impossible without a relation table).

    In the object world, a List<x> in y and a List<y> in x can do that, without any “relational” entity.

    EDIT

    If you want a common “Relation” table, you could maybe create a new entity (with a Guid) : Customer and Location would inherit from that new entity, and attribute entity would be linked with that new entity.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm using the Code First approach and have the following Model: public class Person
I have the following domain model setup using EF code first: public class User
I have the following code: using System; using System.Linq; using System.Linq.Expressions; public class Program
I have the following table created using Entity Framework Code First approach. How do
I'm getting an error when using Entity Framework - Code First in the following
Using the following code: if (isset($_POST['delete']) && isset($_POST['id'])) { $first = get_post('first'); $query =
The following code (without showing it all) is my first attempt at using classes.
Using following code I try to get updated list of checkbuttons' corresponding text values,
I am using following code to get bitmap from url. This function is used
We're using EF Code first, and have a data context for our sales database.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.