EntityFramework学习的入门示例:CodeFirstExistingDatabaseSample

使用CodeFirst方式生成访问数据库代码

下面是BlogMap类代码:

using System.ComponentModel.DataAnnotations.Schema;
using System.Data.Entity.ModelConfiguration;

namespace CodeFirstExistingDatabaseSample.Models.Mapping
{
    public class BlogMap : EntityTypeConfiguration<Blog>
    {
        public BlogMap()
        {
            // Primary Key
            this.HasKey(t => t.BlogId);

            // Properties
            this.Property(t => t.Name)
                .HasMaxLength(200);

            this.Property(t => t.Url)
                .HasMaxLength(200);

            // Table & Column Mappings  http://keleyi.com
            this.ToTable("Blogs");
            this.Property(t => t.BlogId).HasColumnName("BlogId");
            this.Property(t => t.Name).HasColumnName("Name");
            this.Property(t => t.Url).HasColumnName("Url");
        }
    }
}


执行结果:

Codefirst

 完整代码请下载:

https://files.cnblogs.com/jihua/CodeFirstExistingDatabaseSample.rar

当然,如果觉得这个实例比较简单的话,还可以到http://keleyi.codeplex.com/下载一个更实际的例子。

posted on 2013-05-06 19:36  计划  阅读(1182)  评论(0编辑  收藏  举报