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 6702891
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T07:06:24+00:00 2026-05-26T07:06:24+00:00

I have few Domain Models – Address , Customer , Employee , StoreLocation .

  • 0

I have few Domain Models – Address, Customer, Employee, StoreLocation. Address has many to one relationship with Customerand Employee and one to one relationship with StoreLocation.

public class Address
{
    public int Id;
    public string Line1 { get; set; }
    public string Line2 { get; set; }
    public string Line3 { get; set; }
}

public class Customer
{
    public int Id { get; set; }
    public string Name { get; set; }
    public IList<Address> Addresses { get; set; }
}

public class StoreLocation
{
    public int Id { get; set; }
    public string ShortCode { get; set; }
    public string Description { get; set; }
    public Address Address { get; set; }
}


public class Employee
{
    public int Id { get; set; }
    public string Name { get; set; }
    public DateTime Dob { get; set; }
    public IList<Address> Addresses { get; set; }
}

How to Map this relationship?. I am using ASP.NET MVC 3.0 and Entity Framework 4.1.

  • 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-05-26T07:06:24+00:00Added an answer on May 26, 2026 at 7:06 am

    If you are using code-first (I think you want this, else, you have to edit your Q), the first way is the way explained below:

    Entities:

    public class Address {
        [Key]
        public int Id { get; set; }
        public string Line1 { get; set; }
        public string Line2 { get; set; }
        public string Line3 { get; set; }
    
        public virtual Customer Customer { get; set; }
        public virtual StoreLocation StoreLocation { get; set; }
        public virtual Employee Employee { get; set; }
    
        public int? CustomerId { get; set; }
    
        public int? EmployeeId { get; set; }
    }
    
    public class Customer {
        [Key]
        public int Id { get; set; }
        public string Name { get; set; }
        public virtual ICollection<Address> Addresses { get; set; }
    }
    
    public class StoreLocation {
        [Key]
        public int Id { get; set; }
        public string ShortCode { get; set; }
        public string Description { get; set; }
        public virtual Address Address { get; set; }
    }
    
    
    public class Employee {
        [Key]
        public int Id { get; set; }
        public string Name { get; set; }
        public DateTime Dob { get; set; }
        public virtual ICollection<Address> Addresses { get; set; }
    }
    

    DbContext inherited class:

    public class ManyOneToManyContext : DbContext {
    
        static ManyOneToManyContext() {
            Database.SetInitializer<ManyOneToManyContext>(new ManyOneToManyInitializer());
        }
    
        public DbSet<Address> Addresses { get; set; }
        public DbSet<Customer> Customers { get; set; }
        public DbSet<StoreLocation> StoreLocations { get; set; }
        public DbSet<Employee> Employees { get; set; }
    
        protected override void OnModelCreating(DbModelBuilder modelBuilder) {
    
            modelBuilder.Conventions.Remove<IncludeMetadataConvention>();
    
            modelBuilder.Entity<Customer>().HasMany(c => c.Addresses).WithOptional(a => a.Customer).HasForeignKey(a => a.CustomerId);
    
            modelBuilder.Entity<StoreLocation>().HasRequired(s => s.Address).WithOptional(a => a.StoreLocation).Map(t => t.MapKey("AddressId"));
    
            modelBuilder.Entity<Employee>().HasMany(e => e.Addresses).WithOptional(a => a.Employee).HasForeignKey(e => e.EmployeeId);
        }
    }
    

    Context Initializer:

    public class ManyOneToManyInitializer : DropCreateDatabaseAlways<ManyOneToManyContext> {
        protected override void Seed(ManyOneToManyContext context) {
    
        }
    }
    

    That will create the db-schema below:
    Many one-to-many relationship
    Let me know if you have any questions or need clarifications on any part.

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

Sidebar

Related Questions

I have two domain classes: Contract and Orgainisation . A contract has one contractor
I have a few areas in my ASP MVC project. One area has near
I have a few domain classes, and I want to be able to populate
I have the following domain object: public class DomainObject<T,TRepo> where T : DomainObject<T> where
I have the following models. # app/models/domain/domain_object.rb class Domain::DomainObject < ActiveRecord::Base has_many :links_from, :class_name
We have a few objects in our domain model with what you would comically
I have a grails project that contains a few domain objects. I am using
I have few asynchronous tasks running and I need to wait until at least
I have few different applications among which I'd like to share a C# enum.
I have few question in this regard When you create an internet page, does

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.