Adding the SQL Designer Table and Column Description as the class and property descriptions in Entity Framework 5 - 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!

Related

Association Table data with breeze and entity framework

I have a entity framework database first set up and I'm having issues getting data from a table EF treats as an association because it's basically a navigation property. I have a Survey table with an EventId(PK), FacilityId, ExitDate, and Status. I also have a SurveyCategories table with CategoryID(PK), Description and a SurvCat table that just has SurveyId and CategoryId as foreign keys. I can get data from other related tables that don't use a middle table like SurvCat, but even following the documentation from the breeze site for navigation properties I cannot get anything loaded into the SurveyCategories array in each Survey object. I checked the metadata and it's showing the navigation property but I get nothing with this code:
var query = EntityQuery.from('Surveys')
.where("facilityId", "eq", whereClause)
.skip(currentPage * 5).take(5)
.expand("Facility")
.expand("SurveyCategories")
.expand("SurveyCite")
.expand("SurveyDL")
.orderBy(orderBy.survey)
.inlineCount(true);
Any help or links would be greatly appreciated.
I think that the answer for this other question can help you tu solve the problem: Error Loading related entities on demand (entityAspect.loadNavigationProperty()).
The N to N relations are not supported in breeze, so you have to use a intermediate entity to do work this.

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.

Unable to save data to Core Data second related entity

I have an iPad app, built with XCode 4.5, Storyboard, Core Data (using MagicalRecord) and iOS 6. I have two Entities, each with multiple attributes. The first entity has a one to many relationship with the second entity.
In the MagicalRecord docs, I don't see how to persist the data to the second entity; I read somewhere that Core Data generates it's own key and indexes. I know from past use of SQLite that I would need to set the key from the first entity to be able to access the second entity.
[UPDATED] Here is the modified code but it doesn't work either. I have previously selected a row in didSelectRowAtIndexedPath in another class. I assume that set the localContext. Any ideas why this is not working?
- (IBAction)saveAppointment:(UIButton *)sender {
AppointmentInfo *newAppointment = [AppointmentInfo MR_createInContext:localContext]; // create the entity
newAppointment.aStartTime = selectedStartDate;
newAppointment.aEndTime= selectedEndDate;
[localContext MR_saveNestedContexts];
}
You need to create your Entity in the proper (ie. localContext) context:
[AppointmentInfo MR_createInContext:localContext];
I found the problem... seems that I had the store setup incorrectly... I removed the parent pointer from AppointmentInfo and added the "class" information. Works like a champ now... thank you for your time, tho'.

I'm missing functionalities on SubSonic 3

I'm starting to do some test on SubSonic 3 and I'm missing some stuff.
1st: Where's the Table names constants? The place where we could ask for the same of a certain table using intelisense...
2nd: Same as the above but for the table columns... where are they?
This is very useful mostly when you need to pass those names as string... it you need to refactor your DB we don't need to look through all the code to find where was I using that column!! Once you re-generate the code the compiler tells you!
3rd: Now how can I perform an ExecuteReader on a certain table like I'm used to on 2.x through the Query object? I used this a lot for list where I really don't need the business objects (BO) overhead... When I needed a BO (for showing a grid row details) I create it from the row itself...
I'm using ActiveRecord btw...
Thanks guys!
Alex
1st: Where's the Table names constants? The place where we could ask for the same of a certain table using intelisense...
In Structs.tt find the following line of code at line 47:
<# foreach(var col in tbl.Columns){#>
Add the following code above it:
public static string TableName { get { return "<#=tbl.Name#>"; } }
Now you'll have a property that returns the name of the table.
2nd: Same as the above but for the table columns... where are they?
In the generated Structs.cs file, this is included in the 3.0.0.3 version
3rd: Now how can I perform an ExecuteReader on a certain table like I'm used to on 2.x through the Query object? I used this a lot for list where I really don't need the business objects (BO) overhead... When I needed a BO (for showing a grid row details) I create it from the row itself...
If you're using SqlQuery object you can call ExecuteReader on it. Alternatively you can use Linq syntax to generate return custom shaped objects and they'll get mapped automatically.
1st and 2nd: It's not implemented in the default tt-files.
A similiar question:
SubSonic 3 Simple Query Tool
Problem is that's not a correct implementation if you want the 2.x way - the XColumn properties used to be column objects and not string constants, those were found under the Columns struct. So I hope that check-in will not be accepted and that someone will 2.x-ify it correctly.
Anyway as you can see it seems pretty easy to fix it on your own.

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