我创建了SqlSharpener项目,它应该能够满足您的要求。
例如,给定在 SSDT 项目中定义的这些表:
CREATE TABLE [dbo].[Tasks]
(
[Id] INT NOT NULL PRIMARY KEY IDENTITY,
[Name] VARCHAR(50) NOT NULL,
[Description] VARCHAR(1000) NOT NULL,
[TaskStatusId] INT NOT NULL,
[Created] DATETIME NOT NULL ,
[CreatedBy] VARCHAR(50) NOT NULL,
[Updated] DATETIME NOT NULL,
[UpdatedBy] VARCHAR(50) NOT NULL,
CONSTRAINT [FK_Tasks_ToTaskStatus] FOREIGN KEY ([TaskStatusId]) REFERENCES [TaskStatus]([Id])
)
CREATE TABLE [dbo].[TaskStatus]
(
[Id] INT NOT NULL PRIMARY KEY,
[Name] VARCHAR(50) NOT NULL
)
您可以创建一个 T4 模板来生成实体,如下所示:
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Data.Entity;
namespace SimpleExample.EntityFrameworkCodeFirst
{
public partial class TaskContext : DbContext
{
public TaskContext(): base()
{
}
public DbSet<Tasks> Tasks { get; set; }
public DbSet<TaskStatus> TaskStatus { get; set; }
}
[Table("Tasks")]
public partial class Tasks
{
[Key]
[Required]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public Int32? Id { get; set; }
[Required]
[MaxLength(50)]
public String Name { get; set; }
[Required]
[MaxLength(1000)]
public String Description { get; set; }
[Required]
public Int32? TaskStatusId { get; set; }
[ForeignKey("Id")]
public virtual TaskStatus TaskStatus { get; set; }
[Required]
public DateTime? Created { get; set; }
[Required]
[MaxLength(50)]
public String CreatedBy { get; set; }
[Required]
public DateTime? Updated { get; set; }
[Required]
[MaxLength(50)]
public String UpdatedBy { get; set; }
}
[Table("TaskStatus")]
public partial class TaskStatus
{
[Key]
[Required]
public Int32? Id { get; set; }
public virtual ICollection<Tasks> Tasks { get; set; }
[Required]
[MaxLength(50)]
public String Name { get; set; }
}
}
简单示例解决方案中有此 T4 模板的工作示例。如果有 SqlSharpener 当前无法处理的用例,请随时添加问题,我会看看是否可以添加。