locked
Help in Code-First Entity Framework RRS feed

  • Question

  • i have some classes, and i want to make a  relationship, took a look at other resources, but couldn't find result, in fact i need the simplest way as possible, here is the classes:

    Class Image

    public class Image
    {
        public int ImageId { get; set; }
        public byte[] Image { get; set; }
        public Product Product { get; set; }
    }

    Class Product

    public class Product
    {
        public int ProductId { get; set; }
        public string Name { get; set; }
        public Order Order { get; set; }
        public Image Image { get; set; }
    }

    Class Customer

    public class Customer
    {
        public int CustomerId { get; set; }
        public string Name { get; set; }
        public Order Order { get; set; }
    }

    Class Order

    public class Order
    {
        public int OrderId { get; set; }
        public Customer Customer { get; set; }
        public ICollection<Product> Products { get; set; }
        public ICollection<AnotherOrder> AnotherOrders { get; set; }
    }

    Class AnotherOrder

    public class AnotherOrder
    {
        public int AnotherOrderId { get; set; }
        public ICollection<Order> Orders { get; set; }
    }

    I'm trying to use Code First, as so: OrderMap -> HasRequired(x => x.Customer).WithMany().HasForeignKey(x => x.CustomerId);

    And i need those relationships

    One-To-ZeroOrOne {Image & Product} - Product may have Image or not

    One-To-One {Customer  & Order} - Each Order has 1 Customer

    One-To-Many {Order & Product} - Each Order has many Products

    Many-To-Many {AnotherOrder & Order} - Many anotherOrders have many Orders

    P.S. Please provide working code

    P.S.S. Sorry for my bad english

    Friday, January 24, 2014 6:24 AM

Answers

  • Hello,

    You can define your context as below:

    public class S24Context : DbContext
    
        {
    
            public DbSet<Image> Images { get; set; }
    
            public DbSet<Product> Products { get; set; }
    
            public DbSet<Customer> Customers { get; set; }
    
            public DbSet<Order> Orders { get; set; }
    
            public DbSet<AnotherOrder> AnotherOrders { get; set; }
    
    
            protected override void OnModelCreating(DbModelBuilder modelBuilder)
    
            {
    
                //Set all the entity key to be none identified
    
    
                modelBuilder.Entity<Image>().Property(i => i.ImageId).HasDatabaseGeneratedOption(DatabaseGeneratedOption.None);
    
                modelBuilder.Entity<Product>().Property(p => p.ProductId).HasDatabaseGeneratedOption(DatabaseGeneratedOption.None);
    
                modelBuilder.Entity<Customer>().Property(c => c.CustomerId).HasDatabaseGeneratedOption(DatabaseGeneratedOption.None);
    
                modelBuilder.Entity<Order>().Property(o => o.OrderId).HasDatabaseGeneratedOption(DatabaseGeneratedOption.None);
    
                modelBuilder.Entity<AnotherOrder>().Property(a => a.AnotherOrderId).HasDatabaseGeneratedOption(DatabaseGeneratedOption.None);
    
    
                //One-To-ZeroOrOne {Image & Product} - Product may have Image or not
    
                modelBuilder.Entity<Image>().HasRequired(i => i.Product).WithOptional(p => p.Image);
    
    
                //One-To-One {Customer  & Order} - Each Order has 1 Customer
    
                //modelBuilder.Entity<Order>().HasRequired(o => o.Customer).WithRequiredPrincipal(p => p.Order);
    
                modelBuilder.Entity<Customer>().HasRequired(c => c.Order).WithRequiredPrincipal(o => o.Customer);
    
                //One-To-Many {Order & Product} - Each Order has many Products
    
                modelBuilder.Entity<Order>().HasMany(o => o.Products).WithRequired(p => p.Order).HasForeignKey(o => o.OrderId);
    
    
                //Many-To-Many {AnotherOrder & Order} - Many anotherOrders have many Orders
    
                modelBuilder.Entity<Order>().HasMany(o => o.AnotherOrders).WithMany(a => a.Orders).Map(
    
                    oa => { oa.ToTable("OrderAnotherOrders"); oa.MapLeftKey("OrderId"); oa.MapRightKey("AnotherOrderId"); });
    
            }
    
        }
    

    And for one to many relationship, we need to add a foreign key in details table, so we need to add

    public int OrderId { get; set; }

    in Product entity class.

    If you want to know more about the relationship in code first, please refer to link below:

    http://msdn.microsoft.com/en-us/data/jj591620.aspx#RequiredToRequired

    >> in fact i need the simplest way as possible

    If you are confused with the Fluent API, I suggest you using the model first. We can define the entity class and its scaler properties, associations and even inheritance by using the designer window. For details, please refer to link below:

    http://msdn.microsoft.com/en-us/data/jj205424.aspx

    Regards.


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Friday, January 24, 2014 9:21 AM

All replies

  • Hello,

    You can define your context as below:

    public class S24Context : DbContext
    
        {
    
            public DbSet<Image> Images { get; set; }
    
            public DbSet<Product> Products { get; set; }
    
            public DbSet<Customer> Customers { get; set; }
    
            public DbSet<Order> Orders { get; set; }
    
            public DbSet<AnotherOrder> AnotherOrders { get; set; }
    
    
            protected override void OnModelCreating(DbModelBuilder modelBuilder)
    
            {
    
                //Set all the entity key to be none identified
    
    
                modelBuilder.Entity<Image>().Property(i => i.ImageId).HasDatabaseGeneratedOption(DatabaseGeneratedOption.None);
    
                modelBuilder.Entity<Product>().Property(p => p.ProductId).HasDatabaseGeneratedOption(DatabaseGeneratedOption.None);
    
                modelBuilder.Entity<Customer>().Property(c => c.CustomerId).HasDatabaseGeneratedOption(DatabaseGeneratedOption.None);
    
                modelBuilder.Entity<Order>().Property(o => o.OrderId).HasDatabaseGeneratedOption(DatabaseGeneratedOption.None);
    
                modelBuilder.Entity<AnotherOrder>().Property(a => a.AnotherOrderId).HasDatabaseGeneratedOption(DatabaseGeneratedOption.None);
    
    
                //One-To-ZeroOrOne {Image & Product} - Product may have Image or not
    
                modelBuilder.Entity<Image>().HasRequired(i => i.Product).WithOptional(p => p.Image);
    
    
                //One-To-One {Customer  & Order} - Each Order has 1 Customer
    
                //modelBuilder.Entity<Order>().HasRequired(o => o.Customer).WithRequiredPrincipal(p => p.Order);
    
                modelBuilder.Entity<Customer>().HasRequired(c => c.Order).WithRequiredPrincipal(o => o.Customer);
    
                //One-To-Many {Order & Product} - Each Order has many Products
    
                modelBuilder.Entity<Order>().HasMany(o => o.Products).WithRequired(p => p.Order).HasForeignKey(o => o.OrderId);
    
    
                //Many-To-Many {AnotherOrder & Order} - Many anotherOrders have many Orders
    
                modelBuilder.Entity<Order>().HasMany(o => o.AnotherOrders).WithMany(a => a.Orders).Map(
    
                    oa => { oa.ToTable("OrderAnotherOrders"); oa.MapLeftKey("OrderId"); oa.MapRightKey("AnotherOrderId"); });
    
            }
    
        }
    

    And for one to many relationship, we need to add a foreign key in details table, so we need to add

    public int OrderId { get; set; }

    in Product entity class.

    If you want to know more about the relationship in code first, please refer to link below:

    http://msdn.microsoft.com/en-us/data/jj591620.aspx#RequiredToRequired

    >> in fact i need the simplest way as possible

    If you are confused with the Fluent API, I suggest you using the model first. We can define the entity class and its scaler properties, associations and even inheritance by using the designer window. For details, please refer to link below:

    http://msdn.microsoft.com/en-us/data/jj205424.aspx

    Regards.


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Friday, January 24, 2014 9:21 AM
  • Ok, that reveals most, but also 1 question, if i want to make mapping, with (EntityTypeConfiguratioin<T>) in a different .cs file, then do i must write same relation (ex. image & product) in both class mappings, (in image_map, and in product_map). If not then how do i choose in which mapping file i must identify relationship
    Friday, January 24, 2014 11:04 AM
  • Hello,

    We need to config it at one side.
    For example Customer: Address 1:N

    CustomerMapClass:

            HasMany(c => c.c)
                .WithRequired(a => a.Customer)
                .Map(x => x.MapKey("CustomerId"));
    Or we can config it at AddressMapClass similarly.


     

     


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Monday, January 27, 2014 10:23 AM
  • but i guess it's just optional to write 2 mappings. mean explicitly writing. and it has no negative effects. Is it true?
    Monday, January 27, 2014 6:01 PM
  • >>but i guess it's just optional to write 2 mappings. mean explicitly writing. and it has no negative effects. Is it true?

    Yes, we just need to write only one side.


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Tuesday, January 28, 2014 9:30 AM