POCO Class in EF not working as Expected - visual-studio-2012

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.

Related

MVC Switch from Code First to Database first - alter schema without dropping database

With Entity Framework it is possible to enable migrations and create migration steps. But is there an intermediate way where it is possible to change the models, and take care of database schema changes yourself? I don't want to drop the database, because there are future production schenario's.
Now - without enable migrations - I use a code first, and when I create another property in a DbSet - lets assume for example in table 'ExistingTable' int NewField {get; set;}
And when in SQL I update my schema with
Alter table ExistingTable add column NewField int not null
the database knows existence of the new field, the Entity Framework / C# knows the property, but when running, there is some hidden check that still want's to drop my database because of the model change.
Question: can I overwrite a certain setting, in such a way that intial 'Code First' can be transformed to database first?
Removing the __MigrationHistory table from the database (Azure) did work fine for me. I made my (simple) database changes myself and published the code. It all runs fine. There is an alternative see EF Code First Migrations Deployment to an Azure Cloud Service. For a simple one-way patch (and no change history needed) removing the __MigrationHistory works fine.

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!

Updating a method in Entity Framework

I'm new to Entity Framework and am using Database First. I have inherited a project where I am making changes. I have a table called Bid to which I've added column Auciton_Status_Id. I then updated the .edmx file (whatever that is for) by opening it up and selecting Update Model from Database. I had a .tt file (whatever that is for) in another project which I had to manually update by right clicking on it selecting Run Custom Tool. Problem is that the .tt file in the same project as the .edmx file has an associated .cs file (with a class with signature public partial class Entities : DbContext) (whatever a DBContext is) and it has a method called
public virtual int MakeBid(Nullable<long> lotId,
Nullable<decimal> bidValue,
Nullable<System.DateTime> createdDate,
Nullable<long> bidStatusId,
Nullable<System.Guid> userId)
Problem is I want my new column Auction_Status_Id to be added to this method as well. How do I do that when this is an autogenerated class?
MakeBid is probably a stored procedure, so if you modify it in de database you can refresh the .edmx again.
(and make sure you know what it is for :D).

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.

bind subsonic object collection to Microsoft report (rdlc)

Has anyone been able to use a SubSonic generated collection as a "business object datasource" with Microsoft report (rdlc)? I have generated the SubSonic class code but for some reason the report datasource window is not seeing the class as a potential object collection datasource.
Is there something I need to do for this to work?
Thanks in advance...vsdotnetguy
I have loaded Reporting Service reports from business objects before (loaded via NHibernate -- which isn't exact but close enough for argument sake).
Couple of key points:
1. return your objects in List, even if you are only returning one object.
2. You want FLAT business objects. You might have to go thru a DTO transformation to get that. By flat, I mean the most complex property you can have in a business object is a string and a number (int, decimal, double). If you are expecting to grab a value like this:
myObject.Customer.Name, forget it. Create a CustomerName property.
3. If you need data from multiple places try to break up your reports into subreports. You key off of the datasource key to figure out what data to return to the report.
I'll add more as I remember, it has been a few months since I've done this.
Yes I've done it, you should only need to make sure the project containing your reports references your SubSonic project (obviously :).
Sometimes I've also found that Visual Studio can get a little borked and require a restart before repopulating the datasource window with SubSonic generated objects.
Thx Chris and Adam,
Here is the answer I found.
In my case I wanted to dynamically set the main and subreport datasources at run time using the SubSonic object collections. However, I also wanted to design the report layout using drag and drop of the datasource columns.
But I was unable to design the report using drag&drop because none of my SubSonic collections were showing up in the Website Data Sources.
However, later while I was doing some control binding using the ObjectDataSource control, I noticed that NOW my SubSonic collections were showing up in the Website DataSources window and I could drag and drop the report layout.
So if you are dynamically setting the report datasources at run time and ARE NOT using the ObjectDataSource control already in your project, you MUST add a dummy ObjectDataSource control to one of your aspx pages. This will then make the business object datasources show up in the report designer.

Resources