MVC - 建立模型/实体类,对应到数据库

建的模型需要有PK主关键字,默认为’Id’或者‘实体名Id',如果PK不是这两个,要先添加[Key]到PK上方。

    public partial class BookInfo : BaseEntity
    {
        [Key]
        public string BookId { get; set; }
        public string BookName { get; set; }
    }

 

另外说一点,这个BookInfo类继承了BaseEntity这个实体属性基类,那么这个BookInfo也需要有BaseEntity的字段,要不会报错。

    [Serializable]
    public class BaseEntity
    {
        public int EntityId { get; set; }

        private DateTime _CreateTime = DateTime.Now;

        public DateTime CreateTime
        {
            set { _CreateTime = value; }
            get { return _CreateTime; }
        }
    }

 

数据库表为Book,那么需要定义Mapping,Mapping可以和Domain放一块。

 

并且移除EF的表名公约(移除复数表名的契约 ),在定义数据库连接的时候。

protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            // 移除EF的表名公约 ,移除复数表名的契约 
            modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();

            // 还可以移除对MetaData表的查询验证   防止黑幕交易 要不然每次都要访问 EdmMetadata这个表
            //modelBuilder.Conventions.Remove<IncludeMetadataConvention>(); 
            modelBuilder.Conventions.Remove<System.Data.Entity.Infrastructure.IncludeMetadataConvention>();

            modelBuilder.Configurations.Add(new BookMap());
        }

 

posted on 2014-01-04 16:28 llk8 阅读( ...) 评论( ...) 编辑 收藏

转载于:https://www.cnblogs.com/llk8/p/3505005.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值