In my Entity Framework v4 project, I have a table with two columns that are computed by the database (via triggers, etc.). 开发者_StackOverflow中文版 In order to get EF to properly insert records into the table, I have to manually mark the columns as "Computed" in the EF Storage Model (the StoreGeneratedPattern attribute), which is not supported by the designer -- so I have to make the edits by hand to the XML in the .EDMX file.
The problem is that whenever my database schema changes, and I need to update the storage model in my project, the "Update Model Wizard" overwrites the whole Storage Model section of the .EDMX, eliminating my manual changes. This means that I have to keep a special list of such changes and manually re-apply them EVERY TIME I do an update! For the love of god, this has got to be one of the dumbest glitches in EF.
Anyway, my question for the hive is this... Is there any way to prevent the Storage Model overwrites? Is there a way to flag the columns in the database, so that EF will automatically mark them as computed? As a last resort, is the some REALLY EASY XML tool/technique that can automatically apply the changes for me after every refresh?
THANK YOU, oh learned StackOverflow contributors for easing my pain and helping me with this problem even though I do not yet have any status on the site. Someday, when I have a 4+ digit reputation, I will remember you.
Brian
Update
I've been told by an insider that one solution might be to check out the "Designer Power Pack" (link below), which allows you to customize generation of the storage model. I've only skimmed the info so far, but it looks to me like there may be a day or two learning curve to figure that out. Does anyone have any experience with the Designer Power Pack, or any other ideas?
http://blogs.msdn.com/b/adonet/archive/2010/02/08/entity-designer-database-generation-power-pack.aspx
Thanks again, Brian
According to the EF team at Microsoft, this is currently their biggest complaint from customers and also their highest-priority bug.
They say they have a solution designed and plan to ship it with the next release of Visual Studio, likely VS 2010 SP1, but no date on that.
Unfortunately, no good workaround has come up from any sources, but I'm still open to ideas!
Brian
For googlers that find their way here, this problem can be solved with partial classes now. See this SO Answer for more details. I'm quoting @Mugan in the comments at the top of the snippet.
//The generated class 'MyClass' will always be a partial class.
//This allows you to write second partial class which is marked with
//the necessary data annotations.
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
//make sure the namespace is equal to the other partial class MyClass
namespace MvcApplication1.Models
{
[MetadataType(typeof(MyClassMetaData))]
public partial class MyClass
{
}
public class MyClassMetaData
{
[Required]
public int MyClassId {get;set;}
//...
}
}
Taking a shot in the dark here, but have you considered using a View for the table - minus the two computed columns - and not adding the table to your model?
You could generate your edmx file using an alternative designer such as the one in LlblGenPro v3.0. Off the top of my head I don't know how well it supports calculated columns. But if you ask in the support forums http://www.llblgen.com/tinyforum/ you will get a quick, accurate answer. If by chance this feature isn't supported, it's possible that they can add it in a upcoming release. Their delivery cycle is much shorter than MS's.
By the way the LlblGenPro designer not only supports Entity Framework v4 and V1 but NHibernate, Linq to Sql, and their own LlblGenPro runtime.
I just released a tool that may help you: a 'model comparer' for EFv4 models. It is part of my add-in package Huagati DBML/EDMX Tools for Visual Studio, and shows differences between the database, SSDL, and CSDL layers. You can select individual differences and bring them across to the opposite layer(s), or add differences you don't want to bring across to the ignore list.
When bringing changes across, it only updates the affected portions of the model; unrelated and unchanged portions remain untouched.
You can download it and get a trial license from http://huagati.com/dbmltools/ if you want to try it out.
I was facing the same problem with in my application, I was using the Database first approach which made me face this issue. I have over come this issue by using Code first Approach. You may use that too, Designer sux big time I guess.
You may also use "Reverse Engineer Code First" which is a command from EF Power Tools
, this will help you get the Model ready ASAP. Now you may change the model as per your need or the relations, but the drawback is that you cant have a designer in this approch you have to change every thing by hand. Its not that hard even...
精彩评论