现在的位置: 首页 > 综合 > 正文

EntityFramework实例

2013年01月04日 ⁄ 综合 ⁄ 共 782字 ⁄ 字号 评论关闭

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  www.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

 完整代码请下载:

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

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

抱歉!评论已关闭.