Updatemodel not working in MVC - linq-to-sql

I am using Linq to Sql database model with MVC 2.0
when I try to update my model it not updating value, I try UpdataModel but it's also not working

If what you mean is updating Linq-to-SQL DataContext in .dbml Designer file, then you should just manually delete the modified tables and re-add them onto the designer surface.
Entity Framework's datacontext can be refreshed by choosing the "Update Model from database" from a context menu (but sometimes even EF requires readding items). For Linq-to-SQL this option, unfortunately, doesn't work.

If you mean to say that after dragging and dropping the new table to the dbml layout, you are not able to view the changes. You will have to build the project inorder to use the auto generated linq to sql classes. If this is not what you are looking for, can you elaborate on what kind of error you are getting ?

Related

Refactor Entity Framework code to use views instead of tables?

We are possibly looking at switching our tables, for views in EF 4.3.1.
We are using db first via the edmx file, so it generates our entities and dbcontext.
Has anyone got any tips for remapping our entities from tables to views?
Is this prone to disaster? We've had trouble with updating the edmx file in the past via the designer where the underlying changes weren't reflected deep somewhere within the code and we ended up with missing columns.
Or will views act very similar to tables in the EF world?
Designer handles views in completely different way - first of all all views used by EF through designer are read only unless you map stored procedures or custom SQL commands to insert, update and delete operation for each entity you want to modify.
Normally if you have updatable view you can simply modify SSDL part of EDMX and cheat it to pretend that the view is actually a table but this has two consequences:
You must modify EDMX directly as XML
You must not use Update from database any more because it always deletes whole SSDL part and creates a new one without your changes = you must maintain your EDMX manually or buy some extension for VS which will allow you updating only selected tables.

Entity Framework 4.0 is not Importing my Associations from Sql Server 2008 Express R2 database

I'm trying to use Entity Framework 4.0 in my VS2010 project. I've always used Linq to SQL for my projects with great success, and wanted to try using EF4 for a change. Unfortunately when I go through the steps to add EF4 model to my project, none of my associations are created.
Here are my steps:
Right click on my project in VS2010 solution explorer
Click Add => New Item...
Select 'ADO.NET Entity Data Model'
Select 'Generate from database'
Connect to my SQL 2008 Express R2 database
Check the 'Tables' item
Pluralize or singularize generated object names is CHECKED
Include foreign key columns in the model is CHECKED
Click Finish!
I see all of my tables, but there are no associations between any of them. When using the 'Linq to SQL Classes' item, all of my associations are generated as I would expect. Even when I dumb it down to only include two (associating) tables during the EF4 setup process, the associations still don't show up.
I've tried manually adding the assoications through the EF data model designer, but it generates other errors when debugging the project. The errors indicate that my named database associations are not configured correctly in the database.
UPDATE: I thought it was a bug with EF4, but it appears as though I'm missing something in the constraints / associations. I'm going to rebuild some of my tables from scratch and see if that helps. My db was converted from SQL CE 3.5, so I may have missed something along the way.
Thanks
I thought it was a bug with EF4, but it appears as though I'm missing something in the constraints / associations. I rebuilt some of my tables from scratch and it fixed my problem.

Linq to Sql Mapping

When I modify the structure of the table in Sql Server ,won't it be automatically reflected in the "Dbml" Layout designer ?Each and every time i have to delete the tables in "dbml' layout designer and drag the table from sql server.
It would be nice if you had the option to "refresh" keeping any local customizations that you've made, but the designer doesn't seem to work that way. You can, however, simply make the same updates (by hand) in the designer that you've made to the table by adding/deleting columns from the generated class in the designer.
If I were you I'd start using SqlMetal. SqlMetal is a command line application used for generating LINQ DataContexts. It can generate dbml's or just a set of classes for you to use in your project (it's pretty customizable). So create a batch file that calls SqlMetal and run it every time you make database changes and your project will always be up to date with the database.
If you don't want to run the batch file every time you update the database you could just run it every time you build your application with a pre-build step.
There are a number of ways to keep the L2S model in sync with the underlying database:
1) Delete the table(s)/classes involved from the designer surface and drag them back from the 'server explorer' thing.
...or...
2) Update the classes involved manually in the L2S designer.
...or...
3) Use third party tools with update capability (one such tool is my add-in: http://www.huagati.com/dbmltools/ , also mentioned in the Dec 2009 issue of MSDN magazine http://msdn.microsoft.com/en-us/magazine/ee819138.aspx)
...or...
4) Regenerate the entire DBML file using either the designer or sqlmetal.exe.

LINQ2SQL: If I make changes to DB, how to mirror them in DBML?

I am using LINQ2SQL in my current project. I have a quite a lot of tables ~30. When I create my DBML file I change some of the column names etc for readability.
Recently if I made a change to the table in the underlying Database I just deleted and re-added the table in the DBML file, but this is getting tedious. How can I mimic any changes to the database in the DBML file? (e.g. new column, drop column, new default constraint etc).
Out of the box, Linq-to-SQL has no update feature - amazing, but unfortunately true.
There's two tools I know of that get around this:
PLINQO is a set of CodeSmith code generation templates which handle DBML generation and offer lots of extra features (like generating one file per db entity) - including updates!
The Huagati tools offer updates and enforcing naming conventions for DBML and Entity Framework
Marc
I'm not expecting this to be the correct answer, but I thought I'd chime in anyway.
My approach has been to use the drag-n-drop feature for creating the initial DBML file. Then, any changes I make in my DB are also then made, by hand, in either the designer or in the DBML file (as XML) itself. (You can right-click on the DBML file, select Open With, and choose XML editor.) Sometimes it is much easier/faster to work with its XML instead of messing around in the designer.
I would only consider the deleting and re-adding, as you have been doing, if the changes were significant. For adding a new column, however, I'd suggest working directly with the dbml's XML, it's probably faster.
Good luck!
Welcome to the world of tedious! Unless I missed something, you're doing it the right way.
SubSonic looks like an interesting alternative, and boasts
It will also create your database on the fly if you want, altering the schema as you change your object.
As far as free solutions, there are a couple of blunt instruments that mostly move you away from using the O/R Designer: SQLMetal and Damien Guard's T4 templates.
There are multiple commercial solutions available that offer a lot more features.
The question you have to ask yourself is: Am I using the right ORM? LinqToSql has quite a few significant drawbacks, database change handling being only one of them.
Do not use the Visual Studio 2008 LinqToSql O/R Designer
The drawbacks of adopting Linq To Sql

Linqtosql sync with the database

I am about to use linqtosql in my first asp.net mvc application.
I have come up with a database schema. But the problem is that I may change few of the tables in future. So keeping the model classes in sync with database will be a issue.
I got this link which states the similar situation,
keep LinqToSQL sync with the database
My question is, has any body used the third party tools given in the above post,
do they work properly
www.huagati.com/dbmltools/
www.perpetuumsoft.com/Product.aspx?lang=en&pid=55&tid=linqtosqlsynchronization
Or is there any better approach for this problem.
The "official" approach is to simply delete any out of date tables from the designer then drag the updated table from your Server Navigator back on again. I've been using this method for well over a year now and so long as you make your data context changes at the same time you're updating the database you should be OK. It also gives you extra incentive to make sure you have your database structure in order before continuing.
There is also SQLMetal.
http://msdn.microsoft.com/en-us/library/bb386987.aspx
This is whats in our CreateDBML.bat file
call "C:\Program Files\Microsoft Visual Studio 9.0\VC\vcvarsall.bat" x86
sqlmetal /server:{server-name} /user:{username} /password:{password} /database:{databasename}
/dbml:..\..\Codebase\Domain\CompanyName.ProjectName.Domain\Entities\ProjectName.dbml
/namespace:CompanyName.ProjectName.Domain.Entities /pluralize /views
sqlmetal /code:..\..\Codebase\Domain\CompanyName.ProjectName.Domain\Entities\ProjectName.designer.cs ..\..\Codebase\Domain\CompanyName.ProjectName.Domain\Entities\ProjectName.dbml
pause