从Visual Studio数据库项目中生成Entity Framework模型[英] Generate Entity Framework model from Visual Studio database project

本文是小编为大家收集整理的关于从Visual Studio数据库项目中生成Entity Framework模型的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

我将EF5与数据库优先模型一起使用.以及Visual Studio中的数据库项目,以维护应用程序的SQL Server数据库架构.

要更新EF模型,我正在部署空数据库中的更改...

是否可以从Visual Studio(2012)数据库项目中生成和更新EF模型?

更新: 还可以从dacpac文件生成它是一个不太糟糕的选择.有可能吗?

更新: 在MS Build 2014会议上,ADO.NET团队建议,EF等EF的未来仅将使用代码的第一种方法.

稍后,尽管代码库建模,但他们不应首先阐明新方法的名称.也许并不完全相同,而是关于似乎与我非常相似.

所以我将尝试 @ADAM0101解决方案.以CodeFisrt结尾的任何其他提议的解决方案都追求从SSDT项目迁移到EF的CodeFISRT项目,而我想要的是两者的平稳共存(也许我是梦想家...).

推荐答案

我创建了项目 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; }
    }
}

有一个简单示例示例解决方案中.如果SQLSHARPENER当前没有处理的用例我看看是否可以添加.

本文地址:https://www.itbaoku.cn/post/683041.html

问题描述

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 the EF model, I'm deploying the changes in a empty database...

Is it possible to generate and update a EF model from a Visual Studio (2012) database project?

UPDATE: Also generate it from a dacpac file is a not too bad option. Is it possible?

UPDATE: In the MS Build 2014 Conference, the ADO.NET team suggested that the future releases from EF, like EF7, will only work with the Code First approach.

Later, they clarify the name of the new approach should not be Code First, despite Code base modelling. Maybe is not exactly the same but as far as I read about it seems quite similar to me.

So I'm going to try @adam0101 solution. Any other proposed solution that ends with CodeFisrt pursues migrate from the SSDT project to EF's CodeFisrt project, and what I want is a smooth coexistence of both (maybe I'm a dreamer...).

推荐答案

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> 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; }
    }
}

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.