How do I stop the linq designer overwriting my manual changes? - linq

I like Linq but find that once the designer has created my classes I have to modify them.
Then when I change my database and recreate my classes in the designer my changes get wiped.
For instance, let's say I have a class called Person.
I create this class, add some non database related methods to it (outside of Linq) and then create a database table with a similar name.
Linq will duplicate this class in the designer.cs file when I drag it in.
So I go and add the partial keyword to the class in that file or even change the class name Linq created.
As development proceeds, no doubt the database table changes and I have to redrag my tables into the designer.
Voila, my manual changes get wiped.
This is most annoying!
In fact, its unworkable to keep modifying code I have already modified.
Any suggestions?

Create a partial class for your context and make your changes there, the designer generated .cs files are recreated every time you click save.

Related

How to use an IEnumerable<T> business object as RDLC datasource, the way it REALLY works?

I have the hardest time creating an WebForms RDLC report with my business objects, in Visual Studio 2010. I am using the designer to build my report.
In the Report Data View of VS2010 I select New/Dataset... Strange enough, the Data Source Configuration Wizard immediately pops up, covering the Dataset Properties dialog.
When I cancel the Wizard, I am able to manipulate the Dataset Properties dialog somewhat, but none of my business objects does show up. Actually, nothing selectable shows up at all.
What do I need to have my business object selectable?
Note: Earlier in the project, this once worked. I must have changed something, but what?
I have read this question, but my Dataset is in the same project, on the ASPX-Page's code behind file. I have set it up like so:
public IEnumerable<MyDto> Getxy() {
return new List<MyDto>(); //TODO later use real data.
}
I have also read this one but my object already matches what's suggested there. It is a really simple POCO DTO's with no constructor (which makes it automatically have a public parameterless constructor)

MVC Structure?

I am having some difficulty getting my head around the MVC structure.
What I am still struggling with is, when creating the Control to accompany the supporting code, does one need to include the Object within IB?
Think of IB as a freeze-dried version of an object. It is the object itself in visual form. It's stored in the xib file and pulled out when referenced.
You don't NEED to use IB. You can create an object and add it to a view without ever opening the visual tool (some people prefer to work that way).
Storyboarding carries this to an even greater level where you can create the interaction between views in the form of segues.

What's the best alternative to DropCreateDatabaseIfModelChanges (MVC3)?

I've already checked the other questions and searched in google, but I found a lot of solutions...
What is the best?
I need to Alter the table when the DataModel changes, and I need an automatic Solution (like the DropCreateDatabaseIfModelChanges does..
Thanks a lot for all replies
You can go for CodeFirst Migrations. CodeFirst Migrations. You Need EF 5 binaries.
Run the Enable-Migrations –EnableAutomaticMigrations command in Package Manager Console
Add-Migration will scaffold the next migration based on changes you have made to your model.
Update-Database will apply any pending changes to the database.
To update the Entity Framework EDMX and sync it with your DB changes you double click on the EDMX in the VS2010 Solution Explorer. This brings up the designer (Model Browser window apparently is what it’s called) with a class layout of your entities. Right click anywhere on the designer surface and you’ll see an option in the dialog to “Update Model from Database.” Click on this and you get brought into the Update Wizard where you can choose the database objects you want to Add (if new), but by default it refreshes existing objects.
One thing the wizard doesn’t seem to do is delete columns from existing objects. Probably some sort of safety mechanism, but if you deleted the column from the database, do you really want to keep it in your entity? Ah well, who knows.
It don't works well all the time. In my case I try to create nothing manually with the model editor so each time I modify my database model, I open the EDMX editor, I select all entities and I delete them then, I right click the model and I select option : Update from database.
When the object list appear, I select all table and next. It update the model.

Could not find the conceptual model type

I have two Entity Data models within a MVC3 project A and B.
I have recently added the new entity data model B to deal with some new functionality, the issue is that now the existing code has stopped working and I am getting the following error when trying to access code within entity model A.
The error message is:
Could not find the conceptual model type 'project.models.Bclass'
I do not understand why it is this new functionality has affected the current code seeing as entity model A has not change in any way. And functionality B is in a separate class and does not interfere with model A.
When the new code is removed from the project and entity model B is excluded then the code works as it should.
Here is some of the stacktrace from the error:
Exception : Could not find the conceptual model type for 'Project1.Models.CrossSession'.
Application Class - method : System.Data.Metadata.Edm.MetadataWorkspace -- GetEdmSpaceType
User : temp.user
Url : http://localhost/project1/auth/message
Stacktrace : at System.Data.Metadata.Edm.MetadataWorkspace.GetEdmSpaceType(StructuralType objectSpaceType)
at System.Data.Entity.Internal.InternalContext.UpdateEntitySetMappings()
at System.Data.Entity.Internal.InternalContext.TryUpdateEntitySetMappingsForType(Type entityType)
at System.Data.Entity.Internal.InternalContext.GetEntitySetAndBaseTypeForType(Type entityType)
at System.Data.Entity.Internal.Linq.InternalSet`1.Initialize()
at System.Data.Entity.Internal.Linq.InternalSet`1.get_InternalContext()
Solution found. It appears in some cases (randomly) there is a mix up in the code generated by the entity framework. This is apparently a known bug and a fix should be coming in future releases. In the mean time the way around this is to move the efm to a separate class within the solution.
One possible explanation is that if you use the database first approach and forget to add the Entity classes, you will get this error.
i.e. (from the guide linked below)
Right click on the model’s designer surface.
From the context menu, choose Add Code Generation Item.
In the Add New Item dialog that opens, select Data from the list of installed templates types on the left.
Choose the ADO.NET DbContext Generator then click the Add
button.
I normally use Julie Lerman's guide; https://msdn.microsoft.com/en-us/library/jj206878(v=vs.113).aspx
Solved this by updating EntityFramework to the latest version (6.0)
http://nuget.org/packages/EntityFramework/.
After installing it regenerate the models and it will work ;)
I had the same problem with 2 different .edmx files in the same project.
For the first .edmx model I had used "Add Code Generation Item", for the second model I hadn't. The project was building correctly but throwing that exception on runtime.
I solved generating the .tt classes for the second .edmx model as well.
Well I just installed EF 4.1 and tried to combine DbContext with a EMDX - When I tried to pass back a view I got the same error - that's why I ended up here... I think the way around this is to separate the functionality out into separate asemblies in the solution...
This can also be resolved by generating the new edmx in visual studio 2010 so it matches the older ones... (And copying over the files it generates)
Using Visual Studio 12, the issue has still not been resolved. As an alternative workaround, I set up the second SQL data model using "old fashioned" Data Classes that pre-date entity models - they're still there and they still work. Use Add - New Item - (Data Category) - LINQ To SQL Classes, open the ServerExplorer, locate the server and database, then drag and drop it onto the data classes pane. Old school - but conveniently overcomes this highly inconvenient issue when the full functionality of entity models is not needed.
I removed the edmx models, removed the Entity package, re-install the entity 5.0 and works.
I was working with some legacy software and was not keen on upgrading EF. For me, the below setting on my newly added .edmx needed to be changed to match the .edmx that already existed in the project. My new one said "T4" and I changed it to "Legacy Object Context". I did have to delete the .tt templates that were generated previously in order to get it to build. Otherwise, the old generated files stay there and the new files are also there, causing "property already defined" errors. Thx to all the answers above that helped me figure this out!
(Properties panel, after clicking in the entity designer)

Visual Studio Custom project Item

This is more of a question where to look that how to do it..
What I am trying to do is create a custom project item for VS studio, this will have custom code generators etc and will work in the following way..
When I add my new item lets call its a bob item, a few files will be created, myFile.bob, myFile.vb for example. the bob file will hold XML the a custom tool will to geneate the code in myFile.vb or c# etc.
Like some of the database type i will attach a database connection to the bob file, and the drag and drop from the server explorer stored procedures on to the bob dign screen.
this i wll generate a box which will display the procedure, and its parameters (like dropping a table onto the dbml designer), properties will be available for the whole object and also for each parameter to allow the user to change values.
I also want to be able t drag and drop other procedure onto existing ones to create a call stack.
when I add a procedure and or compile a custom tool will use the XML in the bob file to create a class that contains the code to access our data layer for each stored procedure added to the designer.
I am looking to do this in VB2008 and later in VB2010, so I do I create the custom disgnук page!
I hope this make sense.

Resources