Code of first model of cartography

I have a similar structure like below.

public class Price
{
   public decimal Amount {get; set;}
   public string Currency {get; set;}
}

public class Product : BaseEntity
{
   public int Id {get; set;}
   public string Name {get; set;}
   public Price Price {get; set;}
}

I want a Product table in database that will apart Price to its properties. For instance;

**ProductTable**
--Id
--Name
--Amount
--Currency

Then when I get a product from database, it will automatically bind Amount
and Currency
to Price
object of the Product
. How should I make this structure by using Entity Framework Code First ?

Price property have to be virtual, and it should work:

public class Product
{
   public int Id {get; set;}
   public string Name {get; set;}
   public virtual Price Price {get; set;}
}

EDIT:

You can specify custom column name by override OnModelCreating
method in your DbContext
class:

protected override void OnModelCreating(DbModelBuilder modelBuilder)
 {
    modelBuilder.Entity().Property(u => u.Price.Amount)
                                       .HasColumnName("Amount");
 }
Hello, buddy!责编内容来自:Hello, buddy! (源链) | 更多关于

阅读提示:酷辣虫无法对本内容的真实性提供任何保证,请自行验证并承担相关的风险与后果!
本站遵循[CC BY-NC-SA 4.0]。如您有版权、意见投诉等问题,请通过eMail联系我们处理。
酷辣虫 » 后端存储 » Code of first model of cartography

喜欢 (0)or分享给?

专业 x 专注 x 聚合 x 分享 CC BY-NC-SA 4.0

使用声明 | 英豪名录