none
code first change the column name RRS feed

  • Question

  • hi

    I use EF6 c# VS2013 MVC5

    I need to create some profile for my users using code first

    profile will content FirstName, LastName, BillingAddress and ShippingAddress

    for both addresses I use a new table and just add the id of address to AspNetUsers table

    the problem is when I create migrations BillingAddress and ShippingAddress change the name as BillingAddress_AddressId and ShippingAddress_AddressIdi don't like it and I want to generate the column as BillingAddress and ShippingAddress

    can you please help me to do that?

        public class Address
        {
            [Required]
            public int AddressId { get; set; }
    
            [Required]
            [StringLength(255)]
            public string Street { get; set; }
    
            [Required]
            [StringLength(50)]
            public string City { get; set; }
    
            [Required]
            [StringLength(50)]
            public string State { get; set; }
    
            [Required]
            [StringLength(50)]
            public string Contry { get; set; }
    
            [Required]
            public int ZipPostCode { get; set; }
        }

        public class ApplicationUser : IdentityUser
        {
            [Required]
            [Column("SippingAddress")]
            public Address ShippingAddress { get; set; }
    
            [Required]
            [Column("BillingAddress")]
            public Address BillingAddress { get; set; }
            
            public string FirstName { get; set; }
    
            public string LastName { get; set; }
        }

    Sunday, March 15, 2015 3:47 AM

Answers

  • Hello Zorig,

    >>I want to generate the column as BillingAddress and ShippingAddress

    After you added the Address type in the ApplicationUser entity, the Entity Framework would build a 1-1…0 relationship between two entities, you could use the ForeignKey attribute to specify the generated foreign key column name, if your ApplicationUser and Address have a one…zero to many relationship.

    public class ApplicationUser
    
        {
    
            [Required]
    
            public int SippingAddressID { get; set; }
    
            [Required]
    
            [ForeignKey("SippingAddressID ")]
    
            public Address ShippingAddress { get; set; }
    
            [Required]
    
            public int BillingAddressID { get; set; }
    
            [Required]
    
            [ForeignKey("BillingAddressID ")]
    
            public Address BillingAddress { get; set; }
    
    
            public int ApplicationUserID { get; set; }
    
    
            public string FirstName { get; set; }
    
    
            public string LastName { get; set; }
    
        }
    

    If they are one..zero to one relationship, in EF, this kind relationship is implemented by mapping primary key, in your case, we cannot specify one primary property to map to two different columns in database.

    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.

    • Marked as answer by Zorig Monday, March 16, 2015 6:47 PM
    Monday, March 16, 2015 8:50 AM
    Moderator

All replies

  • Hello Zorig,

    >>I want to generate the column as BillingAddress and ShippingAddress

    After you added the Address type in the ApplicationUser entity, the Entity Framework would build a 1-1…0 relationship between two entities, you could use the ForeignKey attribute to specify the generated foreign key column name, if your ApplicationUser and Address have a one…zero to many relationship.

    public class ApplicationUser
    
        {
    
            [Required]
    
            public int SippingAddressID { get; set; }
    
            [Required]
    
            [ForeignKey("SippingAddressID ")]
    
            public Address ShippingAddress { get; set; }
    
            [Required]
    
            public int BillingAddressID { get; set; }
    
            [Required]
    
            [ForeignKey("BillingAddressID ")]
    
            public Address BillingAddress { get; set; }
    
    
            public int ApplicationUserID { get; set; }
    
    
            public string FirstName { get; set; }
    
    
            public string LastName { get; set; }
    
        }
    

    If they are one..zero to one relationship, in EF, this kind relationship is implemented by mapping primary key, in your case, we cannot specify one primary property to map to two different columns in database.

    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.

    • Marked as answer by Zorig Monday, March 16, 2015 6:47 PM
    Monday, March 16, 2015 8:50 AM
    Moderator
  • thanks for your replay

    look like I have to find an other way

    Monday, March 16, 2015 6:48 PM