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

查看:226
本文介绍了从Visual Studio数据库项目生成实体框架模型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用具有数据库优先模型的EF5。并在Visual Visual Studio中的一个数据库项目来维护一个应用程序的Sql Server数据库模式。



要更新EF模型,我将更改部署到一个空数据库...



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



更新:
也可以从 dacpac 文件生成它不是太糟糕的选项。是否可能?



更新:
在MS Build 2014会议中,ADO.NET团队建议将来的版本EF,像EF7一样,只能使用Code First方法。



之后,他们澄清了新方法的名称不应该是Code First,尽管代码库建模。也许不完全一样,但就我所看到的关于它似乎非常类似于我



所以我要尝试@ adam0101解决方案。以CodeFisrt结尾的任何其他提出的解决方案都将从SSDT项目迁移到EF的CodeFisrt项目,我想要的是两者的平滑共存(也许我是一个梦想家...)。

解决方案

我创建了项目 SqlSharpener ,应该能够做你所要求的。



例如,给定在SSDT项目中定义的这些表:

  CREATE TABLE [dbo]。[任务] 

[Id] INT NOT NULL主键标识,
[名称] VARCHAR(50)NOT NULL,
[描述] VARCHAR(1000)NOT NULL,
[TaskStatusId] INT NOT NULL,
[创建时间] DATETIME NOT NULL,
[CreatedBy] VARCHAR(50)NOT NULL,
[更新] DATETIME NOT NULL,
[已更新] VARCHAR(50)NOT NULL,
CONSTRAINT [FK_Tasks_ToTaskStatus] FOREIGN KEY([TaskStatusId ])参考[TaskStatus]([Id])


CREATE TABLE [dbo]。[TaskStatus]

[Id] INT NOT NULL主键,
[名称] VARCHAR(50)NOT NULL

创建T4模板t帽子会生成这样的实体:

  using System; 
使用System.Collections.Generic;
使用System.ComponentModel.DataAnnotations;
使用System.ComponentModel.DataAnnotations.Schema;
使用System.Data.Entity;

命名空间SimpleExample.EntityFrameworkCodeFirst
{
public partial class TaskContext:DbContext
{
public TaskContext():base()
{
}
public DbSet< Tasks>任务{get;组; }
public DbSet< TaskStatus> TaskStatus {get;组;
}


[表(任务)]
public partial class任务
{

[Key]
[必需]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public Int32? Id {get;组;

[必需]
[MaxLength(50)]
public String Name {get;组;

[必需]
[MaxLength(1000)]
public String描述{get;组;

[必需]
public Int32? TaskStatusId {get;组; }
[ForeignKey(Id)]
public virtual TaskStatus TaskStatus {get;组; }

[必需]
public DateTime?创建{get;组;

[必需]
[MaxLength(50)]
public String CreatedBy {get;组; }

[必需]
public DateTime?更新{get;组;

[必需]
[MaxLength(50)]
public String UpdatedBy {get;组; }
}

[表(TaskStatus)]
public partial class TaskStatus
{

[Key]
[必需]
public Int32? Id {get;组; }
public virtual ICollection< Tasks>任务{get;组;

[必需]
[MaxLength(50)]
public String Name {get;组; }
}
}

有一个工作示例 ://github.com/aeslinger0/sqlsharpener/tree/master/examples/SimpleExamplerel =nofollow>简单的示例解决方案。如果存在SqlSharpener目前无法处理的用例,请随时添加问题和我会看看是否可以添加它。


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.

这篇关于从Visual Studio数据库项目生成实体框架模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆