用的GUID和实体框架工作 [英] Working with GUIDs and Entity Framework

查看:89
本文介绍了用的GUID和实体框架工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个问题强调了不能使用服务器端生成的GUID与实体框架。但是,我想的GUID的生成在数据库API的DAL级进行处理(例如,当一个实体的构造函数被调用,我想实体的ID被初始化到一个新的GUID)。我的计划是写一个小工具来生成一个一堆是实体部分类code文件。我有办法做到这一点,问题是:我是不是出了我的脑海做这种方式或者这是我应该做的方式?

This question highlights that you can not use server side generated GUIDs with the entity framework. But, I want the generation of the GUID handled at the DAL level of Database API (ie, when an entity's constructor is called, I want the id of the entity to be initialized to a new GUID). My plan is to write a small tool to generate a bunch of code files that are partial classes of the entities. I have a way to do it, the question is: Am I out of my mind for doing it this way or is this the way I should be doing it?

我的问题是,当EDMX文件更新,我不希望有还编辑了一堆code文件,我只是想运行,将尽一切必要的工具。

My issue is, when the edmx file is update, I don't want to have to also edit a bunch of code files, I just want to run a tool that will do what is necessary.

再次就是我的脑袋上直?

Again, Is my head on straight?

推荐答案

那么,如果你看一下部分类的实体框架生成默认情况下,没有默认的构造函数。

Well if you look at the partial classes the Entity Framework generates by default, there is no default constructor.

因此​​,在一个单独的部分类这样做会很好地工作:

So doing this in a separate partial class will work nicely:

public partial class Customer{
    public Customer(){
         _ID = Guid.NewGuid();
    }
}

所以有可能是没有理由不这样做类似的东西您计划。

So there is probably no reason not to do something like you are planning.

您可能想寻找到T4模板要做到这一点,虽然。这是怎么EF 4.0(即EF在.NET 4.0中),您可以自定义生成的code。现在,而在4.0的经验是相当无缝,你可以很容易地把东西一起基于T4只是为了创造这个谐音类,将工作只是罚款,.NET 3.5 SP1。

You might want to look into T4 templates to do this though. That is how EF 4.0 (i.e. EF in .NET 4.0) allows you to customize the generated code. Now while in 4.0 that experience is quite seemless you could easily put something together based on T4 just to create this partials classes that will work just fine in .NET 3.5 SP1.

希望这有助于

亚历

这篇关于用的GUID和实体框架工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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