EDMX Updates clears model class data annotation - asp.net-mvc-5

I have EDM model classes generated by entity FrameWork DB first approach. Everytime I update EDMX all the classes that have I added validation,schema, attribute names to be displayed are gone.
What is the best approach to handle this situation ?
enter image description here
enter image description here

The generated classes are partial, so you can generate your own properties and methods into a new partial class (in different file) for associate generated class.
Hope my answer could help.

Related

Adding the SQL Designer Table and Column Description as the class and property descriptions in Entity Framework 5

I would like to do the same as described in this post:
Entity Framework 4.1 dynamically retrieve summary for scalar properties from database table column description
I noticed that the user stated that he is using Entity Framework 4.1.
Can anyone confirm whether this feature is part of Entity Framework 5 or 6?
If not, do anybody know of someone that might have added this feature in EF 6, since it is open source now?
Summary
I have added descriptions to the columns and tables in MS SQL Designer. Instead of having to retype these as xml comments for my classes and properties in the generated Entity Framework classes, I would like to have these xml comments generated from the SQL descriptions.
Would something like this not be possible with a T4 template?
My Entity Framework knowledge is very lacking, so if someone can even just point me in the right direction, I am willing to figure this out on my own.
Any advice would be greatly appreciated!
Okay, I have figured out an alternative way.
It's not the answer to the original question, but it will work just as well.
What I have done was entered all the Descriptions from my MS SQL database into the Entity Models Documentation.Summary and Documentation.Long Description.
Then I looked at some code from these two posts:
Example1
Example2
and altered it slightly to work like this:
In the "YourEFModelName".tt file (NOT "YourEFModelName".Context.tt),
I looked for this line:
<#=codeStringGenerator.EntityClassOpening(entity)#>
to find the class header.
Then I added this right in front of it:
<#if (!ReferenceEquals(entity.Documentation, null))
{
#>
/// <summary>
/// <#=entity.Documentation.Summary#>
<#if (!ReferenceEquals(entity.Documentation.LongDescription, null) && !ReferenceEquals(entity.Documentation.LongDescription, ""))
{
#>
/// <#=entity.Documentation.LongDescription#>
<#}#>
/// </summary>
<#}#>
Note that the argument to the "EntityClassOpening" method is "entity", as used in the code you insert into the tt file.
You can use this code for the properties also. To find the properties, just look a bit lower down in the code for this line:
<#=codeStringGenerator.Property(edmProperty)#>
It will generate all your class properties. You can alter the code we used for the class comments to have "edmProperty" (the parameter to the "Property" method in the previous line) instead of "entity".
Then do the same for the Complex Properties and Navigation Properties found just below the edmProperties.
Once I added all my comments into the Entity Framework model and made these changes to the tt file, I right clicked the tt file and chose "run custom tool".
All my generated classes had xml comments with the descriptions I entered in the documentation property of all the classes and properties in my model.
My thanx goes to the people that contributed to the two example threads!

Simperium & mogenerator convenience accessors

I'm using Simperium and mogenerator in my project. I've added SPManagedObject entity to the model and set that as the parent class for other entities. For each entity, mogenerator automatically creates a _<entityname>.h and _<entityname>.m file that has several convenience accessors. I've modified the mogenerator build script so each of these subclasses SPManagedObject instead of NSManagedObject. The entity classes simply import the corresponding _<entityname>.h file.
My problem is I'm getting runtime errors when using any of the convenience accessors. For example, for an entity with attribute of type Integer32 called myInteger, the class file property is NSNumber. Mogenerator creates an accessor named myIntegerValue. The errors I'm getting are that myIntegerValue is an unrecognized selector for SPManagedObject. I can work around this obviously but it seems to be indicative of a bigger problem. Any ideas? Thanks!
This kind of problem is most often the result of failing to configure the class name in the Core Data model editor. You may have a class Foo and an entity Foo but they don't automatically go together (they're not required to have the same name). If you don't do this, you get instances of NSManagedObject instead of instances of your subclass. And of course, NSManagedObject doesn't have those methods.
Select the entity in the model editor and look in the model inspector on the right. Make sure that the class name is configured.

POCO Class in EF not working as Expected

I have created a DataBase in SQL and created an EDMX in Visual Studio 2012. It automatically created POCO (TT) classes. Everything looks fine.
Now I change the column name of a table. I update the EDMX. Opening the EDMX in XML and everything looks fine.
Question 1
After I ran Custom tool in TT, I see that a new property got created additionally, e.g.:
SQL table name : Student
Column name : sName
In my POCO Class
public int sName{ get; set; }
got automatically created.
Now I change the column name in SQL to
Column name : studentName
My POCO class
public int sName{ get; set; }
public int studentName{ get; set; }
Is this a bug or do I need to do something to fix this?
What should I do to avoid this?
Question 2
Also, if I change the data type of any SQL column and update the model from the DB in my EDMX designer, the Conceptual Model is not updated. How do I go about this?
First, to understand your problem, what you need to know is that the EDMX file is just an XML file that contains 3 different sections:
CSDL: Conceptual schema definition language
SSDL: Store schema definition language
MSL: Mapping specification language
The CSDL contains the entities and relationships that make up your conceptual model. The SSDL describes your DB model and the MSL is the mapping between the 2.
The “Update Model From DB” process will update the SSDL (change everything that is inconsistent with the current DB schema), it will only modify the CSDL in case you’ve added new things to your DB schema.
This is quite a normal behavior since your Conceptual schema may/should differ from your DB schema (unless you want your Domain model to look exactly like a DB model which obviously do not sound as OOP/DDD best practices).
As for #Peru, the solution would be to delete the concerned entity (not the entire EDMX!) and then perform the “Update Model From DB” process.
Hope this helps!
Edit:
There's a tool, not for free, which is a Visual Studio plugin that allows you apply changes made into the DB, both on the CSDL and SSDL files: Huagati DBML/EDMX Tools.
The only "free" solution is deleting the entity (or the right field within this entity) that needs to be updated.
Remember that CSDL is supposed to be maintained by developers and must look like an Object Model rather than a DB Model. Imagine you have setup inheritance between your entities or that you have split 1 DB table to 2 EDMX entities, running "Update model from DB" should not overwrite everything!
Personally, I'd open the edmx file as XML and find the problem node and delete it. The file is pretty easy to understand - don't be scared to at least try it out.
I know I'm a bit late here but there is a relatively simple way to generate your POCOs/update your DbContext from the EDMX designer.
Go to your designer, right click on the empty area, click "Update Model from Database", add/update your tables. This will update your edmx XML. After you've verified that your table has been added to the CSDL, SSDL, and MSL sections, right click on your T4 template in the solution explorer (<Name>.tt, not <Name>.Context.tt) and click "Run Custom Tool" - this will generate POCOs for any updated objects. Note that if you are creating new entities, they will not be added to your DbContext class, you will still have to do this manually by adding the following line at the bottom of your class: public virtual DbSet<Entity_Name_Goes_Here> EntityNames { get; set; }
Only the manual edit worked for a View in my project. (going from smallint to decimal(18,2) )
Open the .EDMX file in a text editor, find appropite section, and manually change the Property Type="..." value.
Once you update the fields in Db, Find the respective model.cs file then delete those fields from the model. now Update the EDMX file (Update Model from Database). It worked for me.

Approach for "calculated fields" on NSManagedObject subclass

I'd like to place some custom methods directly into each NSManagedObject. Think, "calculated fields": these methods provide read-only calculated values based upon persistent values on the Entity - which is identical to this question.
I'm using the Xcode New File... Wizard to create NSManagedObject subclasses for each of my Entities. I'm trying to benefit from the system auto-creating the accessors for me. For example:
Core Data Entity: "Site"
#interface Site : NSManagedObject
As I continue to add new Attributes to my Entities, I'm replacing their corresponding NSManagedObjects by using the Wizard. When each file is replaced, my custom methods are lost.
Should I create my custom methods elsewhere so that I can continue to leverage the Wizard? Or, should I keep the methods on the NSManagedObject and add Accessors for new Attributes manually? Is there another approach?
Create a category on your NSManagedObject subclass:
In the "New File ..." dialog, choose "Objective-C category".
Create a category "CustomMethods" on "Site".
Xcode will create files Site+CustomMethods.h, declaring the #interface Site (CustomMethods), and Site+CustomMethods.m for the corresponding implementation.
Add your custom methods to the category.
These files will not be overwritten when you recreate Site.m and Site.h in Xcode.
All category methods can be used as if they had been declared in the class itself. The only thing you can not do in a category is add new instance variables.
Once I have used the wizard to create the initial managed objects, I generally change them manually.
Another way of doing this is to create subclasses of the wizard generated class files and use these.
When they are regenerated, all of your custom code is in the subclass, as opposed to the overwritten class file.

Renaming Core Data class

I have an application that uses CoreData.
I previously had a class named Marker which was linked to the Marker entity in Core Data.
I renamed the Marker class to CoreDataMarker. So I created a new .xcdatamodel file with the new class name for the entity. Then I created a .xcmappingmodel and selected the old and the new .xcdatamodel files and it seemed to 'auto-setup' fine.
However, when I run my application it complains with: "Can't merge models with two different entities named 'Marker'". I understand that this happens, but I have no idea how to solve it.
Do you know how?
Thanks in advance!
You don't need a new xcdatamodel. Change the in the "Class" field in the entity description for your Marker entity to CoreDataMarker. That's all that's needed. The implementing class information does not require a schema migration.
And make sure ONLY the current version of the data model (latest xcdatamodel file) is included in target and mapping model file. It looks like putting other model files are being done automatically based on information from xcmappingmodel file.
It is unintuitive but this was what worked for me.

Resources