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

一、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 | 引用: 0 | 查看次数: 3583
发表评论
登录后再发表评论!