在做MVC+EF CodeFirst 的Demo时,碰到的问题,

在组册用户时,要让用户输入确认密码,但是数据库中又不需要保存这个字段,解决方案很多了,这里我列出通过EF Code First的解决方案。

   UserInfo实体
    public class UserInfoModel
{
[UIHint("HiddenInput")]
public int UserId { get; set; }
[Display(Name = "用户名")]
public string UserName { get; set; }
[Display(Name = "密码")]
[DataType(DataType.Password)]
public string UserPwd { get; set; }
[Display(Name = "确认密码")]
[DataType(DataType.Password)]
public string ConfirmUserPwd { get; set; } public string Email { get; set; }
public DateTime RegisterDate { get; set; }
[Display(Name = "备注")]
public string Memo { get; set; }
public virtual RoleInfoModel Role { get; set; }
}
   //UserInfo的Fluent API  配置类
public class UserInfoConfiguration : EntityTypeConfiguration<UserInfoModel>
{
public UserInfoConfiguration()
{
HasKey(c => c.UserName);
Property(c => c.UserId).HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity); //通过Map也可以忽略
//Map(l =>
//{
// l.Properties(x => new { x.UserId, x.UserName, x.UserPwd, x.Email, x.RegisterDate, x.Memo }); //}); Ignore(c => c.ConfirmUserPwd); //忽略这个属性
}
}
   关于EntityTypeConfiguration<TModel>.Map(),这个方法很强大。
   定义:
           public class EntityTypeConfiguration<TEntityType> : StructuralTypeConfiguration<TEntityType> where TEntityType : class
{
//……省略N行
public EntityTypeConfiguration<TEntityType> Map(Action<EntityMappingConfiguration<TEntityType>> entityMappingConfigurationAction); //…… }



   具体使用在以下5个方面:
   1.可以实现同一个实体映射到不同的表,也就是分割实体。
     现有StudentInfo实体类如下:
    public class Student
{
public Student()
{ }
public string StudentId { get; set; }
public string StudentName { get; set; }
public bool Sex { get; set; }
public DateTime Birthday { get; set; }
public string Email { get; set; }
public string QQ { get; set; }
public string MobilePhone { get; set; }
public string FixedPhone { get; set; }
public byte[] Photo { get; set; }
}

 
    public class StudentConfig : System.Data.Entity.ModelConfiguration.EntityTypeConfiguration<Student>
{
public StudentConfig()
{
HasKey(c => c.StudentId);
Map(c =>
{
c.Properties(l => new { l.StudentName, l.Sex, l.QQ, l.FixedPhone, l.MobilePhone });
c.ToTable("StudentBasicInfo");
});
Map(c =>
{ c.Properties(l => new { l.Photo });
c.ToTable("StudentPhoto");
});
}
}

   生成的数据库表:
    
   

   2.可以实现多个实体映射到同一个表。
现有student和studentphoto实体类:
    public class Student
{
public Student()
{ }

public string StudentId { get; set; }

        public string StudentName { get; set; }
public bool Sex { get; set; }
public DateTime Birthday { get; set; }
public string Email { get; set; }
public string QQ { get; set; }
public string MobilePhone { get; set; }
public string FixedPhone { get; set; }

public StudentPhoto Photo { get; set; }

    }

    public class StudentPhoto
{

public string StudentId { get; set; }

        public byte[] Photo { get; set; }
       

public Student student { get; set; }

    }
 


    public class StudentConfig : System.Data.Entity.ModelConfiguration.EntityTypeConfiguration<Student>
{
public StudentConfig()
{
HasKey(c => c.StudentId);

HasRequired(c => c.Photo).WithRequiredDependent(c => c.student);

            ToTable("student");
}
}
public class StudentPhotoConfig : System.Data.Entity.ModelConfiguration.EntityTypeConfiguration<StudentPhoto>
{
public StudentPhotoConfig()
{
HasKey(c => c.StudentId);
ToTable("student");
}
}

   要实现这个功能要注意以下几点:
1.实体之间要有一对一的关系(实体中绿色字体)
2.实体之间要有完全相同的主键(实体中红色字体)
3.配着类中要指明一对一的关系(StudentConfig 类中红色字体)
4.相同的表名。(ToTable("student");) 
   生成的数据库表:
 
   3.可以实现每个层次结构一张表 (TPH) 继承
   4.可以实现每个类型一张表 (TPT) 继承
   5.可以实现每个具体类型一张表 (TPC) 继承
   
     



Entity Framework Code First 中使用 Fluent API 笔记。的更多相关文章

  1. Entity Framework(七):Fluent API配置案例

    一.配置主键 要显式将某个属性设置为主键,可使用 HasKey 方法.在以下示例中,使用了 HasKey 方法对 Product 类型配置 ProductId 主键. 1.新加Product类 usi ...

  2. Entity Framework Code First实体对象变动跟踪

    Entity Framework Code First通过DbContext.ChangeTracker对实体对象的变动进行跟踪,实现跟踪的方式有两种:变动跟踪快照和变动跟踪代理. 变动跟踪快照:前面 ...

  3. Entity Framework Code First 学习日记(1)精

    我最近几天正在学习Entity Framework Code First.我打算分享一系列的学习笔记,今天是第一部分: 为什么要使用Code First: 近 年来,随着domain driven d ...

  4. Entity Framework 实体框架的形成之旅--Code First模式中使用 Fluent API 配置(6)

    在前面的随笔<Entity Framework 实体框架的形成之旅--Code First的框架设计(5)>里介绍了基于Code First模式的实体框架的经验,这种方式自动处理出来的模式 ...

  5. How to: Use the Entity Framework Code First in XAF 如何:在 XAF 中使用EF CodeFirst

    This topic demonstrates how to create a simple XAF application with a business model in a DbContext ...

  6. Entity Framework Code First属性映射约定

    Entity Framework Code First与数据表之间的映射方式有两种实现:Data Annotation和Fluent API.本文中采用创建Product类为例来说明tity Fram ...

  7. Entity Framework Code First关系映射约定

    本篇随笔目录: 1.外键列名默认约定 2.一对多关系 3.一对一关系 4.多对多关系 5.一对多自反关系 6.多对多自反关系 在关系数据库中,不同表之间往往不是全部都单独存在,而是相互存在关联的.两个 ...

  8. Entity Framework Code First (三)Data Annotations

    Entity Framework Code First 利用一种被称为约定(Conventions)优于配置(Configuration)的编程模式允许你使用自己的 domain classes 来表 ...

  9. Entity Framework Code First (二)Custom Conventions

    ---------------------------------------------------------------------------------------------------- ...

随机推荐

  1. springboot学习笔记(一)

    一.什么是SpringBoot 描述:Spring Boot是Spring社区发布的一个开源项目,旨在帮助开发者快速并且更简单的构建项目.大多数SpringBoot项目只需要很少的配置文件.二.Spr ...

  2. Spring注入JPA+JPA事务管理

    本例实现的是Spring注入JPA 和 使用JPA事务管理.JPA是sun公司开发的一项新的规范标准.在本质上来说,JPA可以看作是Hibernate的一个子集:然而从功能上来说,Hibernate是 ...

  3. MongoDB 数组

    MongoDB是文档型数据库,每个文档(doc)表示数据的一项记录.相比关系型DB的row只能使用简单的数据类型,doc能够使用复杂的数据类型:内嵌doc,数组.MongoDB的数组是一系列元素的集合 ...

  4. HTML5系列:HTML5与HTML4的区别

    1. 语法的改变 1.1 DOCTYPE声明 DOCTYPE声明在HTML文件中必不可少,位于文件第一行. HTML4中声明方法: <!DOCTYPE html PUBLIC "-// ...

  5. CHECK_NRPE: Received 0 bytes from daemon. Check the remote server logs for error messages.

    今天,在用icinga服务器端测试客户端脚本时,报如下错误: [root@mysql-server1 etc]# /usr/local/icinga/libexec/check_nrpe -H 192 ...

  6. iOS开发——高级特性&Runtime运行时特性详解

    Runtime运行时特性详解 本文详细整理了 Cocoa 的 Runtime 系统的知识,它使得 Objective-C 如虎添翼,具备了灵活的动态特性,使这门古老的语言焕发生机.主要内容如下: 引言 ...

  7. HTML5 视频(一)

    HTML5 提供了展示视频的标准 今天,大多数视频是通过插件(比如 Flash)来显示的.然而,并非所有浏览器都拥有同样的插件. HTML5 规定了一种通过 video 元素来包含视频的标准方法. 一 ...

  8. JDBC连接MySQL数据库代码模板

    下面这个例子是最简单的JDBC连接MySQL数据库的例子. 一般步骤: 1.注册驱动: 2.建立连接: 3.创建语句: 4.处理结果: 5.释放资源. 注意: 1.软件开发环境:MyEclipse 8 ...

  9. ECMAScript 5中属性的特性值

    这是<JavaScript高级程序设计(第三版)>第六章相关内容的总结. ECMAScript中有两种属性:数据属性和访问器属性.每种属性都有四个特性值. 数据属性的四个特性值: [[Co ...

  10. js修改不了input的值

    奇怪的input 今天想做一个通过点击按钮,修改input值的控件,但是点击按钮后,input值变成修改的值后又变回了原来的值,百思不得其解,代码如下 <form> <div cla ...