Generate Entity Framework model from Visual Studio database project

后端 未结 1 672
遇见更好的自我
遇见更好的自我 2021-01-04 07:40

I\'m using EF5 with a Database-First model. And a database project in visual Visual Studio to maintain the Sql Server database schema of an application.

To update th

1条回答
  •  星月不相逢
    2021-01-04 08:12

    I created the project SqlSharpener which should be able to do what you're asking.

    For example, given these tables defined in an SSDT project:

    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
    )
    

    You can create a T4 template that will generate the entities as such:

    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 { get; set; }
            public DbSet 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 { get; set; }
    
            [Required]
            [MaxLength(50)]
            public String Name { get; set; }
        }
    }
    

    There is a working example of this T4 template in the simple example solution. If there is a use case that SqlSharpener doesn't currently handle, feel free to add an issue and I'll see if I can add it in.

    0 讨论(0)
提交回复
热议问题