MVC EF代码首先创建模型类 [英] MVC EF code first creating model class

查看:94
本文介绍了MVC EF代码首先创建模型类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚开始使用MVC和EF代码。我正在努力使用EF代码优先方法建模一个房地产公司的DB模型,我做了一些练习以及阅读一些在线教程。



第一件事我有一个客户表,与他/她已经注册的一个或多个属性有关,因为它是所有者出售或出租,我想知道是否有可能在模型类中有一些子类注册属性如下:

  public Property 
{

public int PropertyID {get;组; }
public bool IsforSale {get;组; }
public bool IsforRent {get;组; }

public class Apartment {

public int ApartmentID {get;组; }
public int AptSqureMeter {get;组; }
。 。 。
。 。 。

}

public class Villa {

public int VillaID {get;组; }
public int VillaSqureMeter {get;组; }
。 。 。
。 。 。

}

以及其他类型属性的其他子类
}

如果答案为是,那么我应该如何使用数据注释或Fluent API声明关系,然后请帮助我如何更新客户表和属性表与客户信息和财产信息在同一时间?



感谢您的回答提前。

解决方案

由于@Esteban已经为您提供了如何设计您的POCO并管理它们之间的关系的相当详细的答案,我将仅专注于您的问题的那一部分:


我应该如何使用数据注释或Fluent API声明关系


首先您应该知道某些型号配置只能使用流畅的API完成,这里是一个非详尽的列表:




  • DateTime的精度PR

  • 数字属性的精度和比例

  • 字符串或二进制属性为固定长度

  • 一个String属性为非unicode

  • 关系的on-delete行为

  • 高级映射策略



说,我不是告诉你使用Fluent API而不是Data Annotation: - )



要处理一个MVC应用程序,您应该记住,数据注释属性将被实体框架和MVC所理解和处理,以便验证。但MVC将无法理解Fluent API配置!


I'm new to MVC and EF code first. I'm in struggle to model a real-estate company DB model using EF code-first approach and I did some exercises as well as reading some online tutorials.

First thing I have a customers table that would be in relation with one or more properties he/she has registered as it's owner to sell or to rent, I was wondering if it is possible to have some sub classes inside a model class for registered properties as below:

public Property
{

  public int PropertyID { get; set; }
  public bool IsforSale { get; set; }
  public bool IsforRent { get; set; }

  public class Apartment{

     public int ApartmentID { get; set; }
     public int AptSqureMeter { get; set; }
       .            .            .
       .            .            .

    }

  public class Villa{

     public int VillaID { get; set; }
     public int VillaSqureMeter { get; set; }
       .            .            .
       .            .            .

    }

    and also other sub-classes for other types of properties
}

If the answer is Yes, then how should I declare the relations using data annotation or Fluent API, and then please help me how to update both Customers table and Property table with the customer information and property info at the same time?

thanks for your answer in advance.

解决方案

As @Esteban already provided you with a pretty detailed answer on how to design your POCOs and manage the relationship between them, I will only focus on that part of your question:

how should I declare the relations using data annotation or Fluent API

First of all, you should know that certain model configurations can only be done using the fluent API, here's a non exhaustive list:

  • The precision of a DateTime property
  • The precision and scale of numeric properties
  • A String or Binary property as fixed-length
  • A String property as non-unicode
  • The on-delete behavior of relationships
  • Advanced mapping strategies

That said, I'm not telling you to use Fluent API instead of Data Annotation :-)

As you seem to work on an MVC application, you should keep in mind that Data Annotation attributes will be understood and processed by both by Entity Framework and by MVC for validation purposes. But MVC won't understand the Fluent API configuration!

这篇关于MVC EF代码首先创建模型类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆