欢迎光临

木子屋

标题: Entity Framework Power Tools Beta 2生成代码结构

作者: dnawo

日期: 2012-08-16 06:17 PM

分类: Web编程

内容: 一、test库结构
复制内容到剪贴板 程序代码Create TABLE [dbo].[CategoryTB](
[Id] [int] IDENTITY(1,1) NOT NULL,
[Name] [nvarchar](50) NULL,
CONSTRAINT [PK_CategoryTB] PRIMARY KEY CLUSTERED
(
[Id] ASC
)WITH (IGNORE_DUP_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY]
Create TABLE [dbo].[UserTB](
[Id] [int] IDENTITY(1,1) NOT NULL,
[CategoryId] [int] NOT NULL,
[Usn] [nvarchar](20) NOT NULL,
[Pwd] [nvarchar](20) NOT NULL,
[Created] [datetime] NULL,
CONSTRAINT [PK__UserTB] PRIMARY KEY CLUSTERED
(
[Id] ASC
)WITH (IGNORE_DUP_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY]
GO
Alter TABLE [dbo].[UserTB] WITH CHECK ADD CONSTRAINT [FK_UserTB_CategoryTB] FOREIGN KEY([CategoryId])
REFERENCES [dbo].[CategoryTB] ([Id])
二、Entity Framework Power Tools Beta 2生成代码
1、Mapping
Models\Mapping\CategoryTBMap.cs：
复制内容到剪贴板 程序代码using System.ComponentModel.DataAnnotations;
using System.Data.Entity.ModelConfiguration;
namespace ConsoleApplication1.Models.Mapping
{
public class CategoryTBMap : EntityTypeConfiguration<CategoryTB>
{
public CategoryTBMap()
{
// Primary Key
this.HasKey(t => t.Id);
// Properties
this.Property(t => t.Name)
.HasMaxLength(50);
// Table & Column Mappings
this.ToTable("CategoryTB");
this.Property(t => t.Id).HasColumnName("Id");
this.Property(t => t.Name).HasColumnName("Name");
}
}
}
Models\Mapping\UserTBMap.cs：
复制内容到剪贴板 程序代码using System.ComponentModel.DataAnnotations;
using System.Data.Entity.ModelConfiguration;
namespace ConsoleApplication1.Models.Mapping
{
public class UserTBMap : EntityTypeConfiguration<UserTB>
{
public UserTBMap()
{
// Primary Key
this.HasKey(t => t.Id);
// Properties
this.Property(t => t.Usn)
.IsRequired()
.HasMaxLength(20);
this.Property(t => t.Pwd)
.IsRequired()
.HasMaxLength(20);
// Table & Column Mappings
this.ToTable("UserTB");
this.Property(t => t.Id).HasColumnName("Id");
this.Property(t => t.CategoryId).HasColumnName("CategoryId");
this.Property(t => t.Usn).HasColumnName("Usn");
this.Property(t => t.Pwd).HasColumnName("Pwd");
this.Property(t => t.Created).HasColumnName("Created");
// Relationships
this.HasRequired(t => t.CategoryTB)
.WithMany(t => t.UserTBs)
.HasForeignKey(d => d.CategoryId);
}
}
}
2、Models
Models\CategoryTB.cs：
复制内容到剪贴板 程序代码using System;
using System.Collections.Generic;
namespace ConsoleApplication1.Models
{
public class CategoryTB
{
public CategoryTB()
{
this.UserTBs = new List<UserTB>();
}
public int Id { get; set; }
public string Name { get; set; }
public virtual ICollection<UserTB> UserTBs { get; set; }
}
}
Models\UserTB.cs：
复制内容到剪贴板 程序代码using System;
using System.Collections.Generic;
namespace ConsoleApplication1.Models
{
public class UserTB
{
public int Id { get; set; }
public int CategoryId { get; set; }
public string Usn { get; set; }
public string Pwd { get; set; }
public Nullable<System.DateTime> Created { get; set; }
public virtual CategoryTB CategoryTB { get; set; }
}
}
3、Context
Models\testContext.cs：
复制内容到剪贴板 程序代码using System.Data.Entity;
using System.Data.Entity.Infrastructure;
using ConsoleApplication1.Models.Mapping;
namespace ConsoleApplication1.Models
{
public class testContext : DbContext
{
static testContext()
{
Database.SetInitializer<testContext>(null);
}
public testContext()
: base("Name=testContext")
{
}
public DbSet<CategoryTB> CategoryTBs { get; set; }
public DbSet<UserTB> UserTBs { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Configurations.Add(new CategoryTBMap());
modelBuilder.Configurations.Add(new UserTBMap());
}
}
}

+ 查看当前日志评论 (0)

登录

────────────

木子屋

PJBlog3 v3.2.9.518 Inside.

Processed In 1.449 ms

返回

标题: Entity Framework Power Tools Beta 2生成代码结构

你没有权限发表评论

────────────

木子屋

PJBlog3 v3.2.9.518 Inside.

Processed In 1.449 ms

返回

暂无评论

返回

────────────

木子屋

PJBlog3 v3.2.9.518 Inside.

Processed In 1.497 ms

返回