Has anyone had experience working on an ASP.NET project that contains both files developed using WinForms and files developed using MVC3?
In theory - can it work?
I have a deadline to make this work, but I'm seeing quite some errors now; below is an example.
Note: the MVC3 files were developed seperately using EF4, database first, then auto-generate models and .edmx; and it worked well - now after combining them with some WinForms files into 1 project, it does not work.
Exception
Details: System.Data.Entity.Infrastructure.UnintentionalCodeFirstException: Code generated using the T4 templates for Database First and Model First development may not work correctly if used in Code First mode. To continue using Database First
or Model First ensure that the Entity Framework connection string is specified in the config file of executing application. To use these classes, that were generated from Database First or Model First, with Code First add any additional configuration using
attributes or the DbModelBuilder API and then remove the code that throws this exception.
I searched aorund; here is an answer I found, on the exact same error I encounterd above:
<div class="vote"> </div>
<div class="post-text">
Once you are using model-first / database-first (EDMX) you must use
Entity connection string with referencing .ssdl, .msl and .csdl metadata files.
</div>
The prpblem with my situation is: I need the code developed using MVC3 & EF4 to start using an
existing database, which is
closely tied to the code developed using WinForms - so I had to change the connectionstring in web.config file to point to the database; thus I get this error.
Has anyone had experience working on an ASP.NET project that contains both files developed using WinForms and files developed using MVC3?
In theory - can it work?
Yes. See http://msprogrammer.serviciipeweb.ro/2010/03/29/asp-net-mvc-orm-and-viewmodels/
claudia888
Exception Details: System.Data.Entity.Infrastructure.UnintentionalCodeFirstException: Code generated using the T4 templates for Database First and Model First development may not work correctly if used in Code First mode
This is because your connection string is not in the edmx form - but in code first form.
1. Check your connection string in both winforms and mvc project.
2. Check also Code First generated code for UnintentionalCodeFirstException ( search in your solution) . Do not remove- insted , think how can you generated fluent api code( no template yet for this)
This is because your connection string is not in the edmx form - but in code first form.
Yes - I figured that out; see my post.
1. Check your connection string in both winforms and mvc project.
What I need to do is: combine the 2 projects into 1 solution; and use 1 database. So the connectionstring from the original MVC project has to be changed into using the same connectionstring (that points to the actual db, not its original one with
metadata) ... correct?
2. Check also Code First generated code for UnintentionalCodeFirstException ( search in your solution) . Do not remove- insted , think how can you generated fluent api code( no template yet for this)
By "api code"- are you suggesting that I should not try to combine these 2 projects into 1 solution, but rather keep them seperate and build an api in between them?
The original MVC project has its own .mdf - I'm struggling to somehow add its tables into the database for the WinForms project (which has a much larger database and lot more functionality); then in the web.config file for the MVC project, use the SAME connectionstring.
Is it possible to make this work? Am I on the right track, or is there a better way to approach this?
1. Check your connection string in both winforms and mvc project.
What I need to do is: combine the 2 projects into 1 solution; and use 1 database. So the connectionstring from the original MVC project has to be changed into using the same connectionstring (that points to the actual db, not its original one with
metadata) ... correct?
Both are needed to use same connectionstring - the one with metadata . If you want to use codefirst, you should search for
UnintentionalCodeFirstException and modify the template .tt to generate relationships.
claudia888
2. Check also Code First generated code for UnintentionalCodeFirstException ( search in your solution) . Do not remove- insted , think how can you generated fluent api code( no template yet for this)
By "api code"- are you suggesting that I should not try to combine these 2 projects into 1 solution, but rather keep them seperate and build an api in between them?
No - I mean http://msdn.microsoft.com/en-us/data/hh134698.aspx
More - I think you should use a common dll for edmx and database related stuff - like in http://msprogrammer.serviciipeweb.ro/2010/03/29/asp-net-mvc-orm-and-viewmodels/ - see how I use for a console and a MVC project.
claudia888
The original MVC project has its own .mdf - I'm struggling to somehow add its tables into the database for the WinForms project (which has a much larger database and lot more functionality); then in the web.config file for the MVC project, use the SAME connectionstring.
First CONSOLIDATE the 2 databases -make have the same tables / columns. Then you can re-use edmx and stuff..
( other approach, much harder, is to use interfaces - and make call Repository that use database implementations to save data)
A quick question - re your statement "First CONSOLIDATE the 2 databases -make have the same tables / columns. Then you can re-use edmx and stuff..":
In my case, the 2 databases do NOT have the same tables/columns. The WinForms app was developed 4 years ago, using VS2005 and SQL server 2005; it has a lot of tables and features that I need to utilize and do not want to re-create; the MVC app was just developed
in the last few months, using VWD2010 and SQL server express 2008 -it is a much smaller app, with less tables and logics.
My job is to combine these 2 projects; more specifically: "ADD" the MVC project into the WinForms project. So I did the following:
1) Upgrade the WinForms project's db to 2008 in SQL server express 2008 Management Studio - done, no error;
2) Convert the WinForms project's code in VWD2010, re-target ASP.NET4 framework - almost done; I can build the solution, hit F5 to bring the app interface up, most functions work correctly....1 error left during build that I'm still troubleshooting;
3) Add tables from the MVC project into the db for the WinForms project, define relationships - done;
4) Add the MVC project code as an additional project to the WinForms solution - done; I can compile the overall solution, and the MVC project does not give any error.
5) Make the MVC project code work with the "Additonal tables" that I added in step 3) in the overall DB -
I'm stuck here.... I can bring the MVC project interface up but most pages give errors; I have edited the web.config file for the MVC project, to remove the connectionstring with the metadata,
etc. based on the error messages I got so far - hence I posted this question.
So do you still think that it is possible for me to consolidate the 2 dbs into 1, based on the above info?
Please let me know - thanks so much for your help!
Update - I read the links you provided about Fluent API; plus read more on EF. 2 more questions:
1) Fluent API and Code First - it seems that the benefit of Fluent API is to add extra configuration capability for creating tables/DB, when using Code First; if I do NOT use code first moving forward, just adding/modifying tables "the old
way", and develop my logic to work with the tables ... do I still need to worry about it?
Note: I do want to learn this and use it; but for the current project, I have a deadline and really just want to get things done as quickly as possible.
2) ViewModel - supposed I'm able to conslidate the 2 dbs into 1; can I define a ViewModel between a table from the "original" database (from the WinForms project), and a NEW table that I just added for the MVC project?
3) Add tables from the MVC project into the db for the WinForms project, define relationships - done
claudia888
5) Make the MVC project code work with the "Additonal tables" that I added in step 3) in the overall DB -
I'm stuck here.... I can bring the MVC project interface up but most pages give errors; I have edited the web.config file for the MVC project, to remove the connectionstring with the metadata,
etc. based on the error messages I got so far - hence I posted this question.
It's strange. It should work as long as there are SAME tables ... or did you integrate somehow with existing Winforms tables ?!
Note: I do want to learn this and use it; but for the current project, I have a deadline and really just want to get things done as quickly as possible.
Use the old way in MVC then - no need for code first
.
claudia888
2) ViewModel - supposed I'm able to conslidate the 2 dbs into 1;
In the previous post you just did.
claudia888
can I define a ViewModel between a table from the "original" database (from the WinForms project), and a NEW table that I just added for the MVC project?
You said: "First CONSOLIDATE the 2 databases -make have the same tables / columns. Then you can re-use edmx and stuff..":
In my case, the 2 databases do NOT have the same tables/columns.
So - is it possible for me to consolidate these 2 databases into 1?
I don't need to re-use the .edmx from the MVC project; or stick with code first.
I just want to add the neccessary tables from the MVC project to the database from the WinForms project, and make the MVC project be able to access the database.
Now, for most pages from the MVC project, this is what I see:
Code generated using the T4 templates for Database First and Model First development may not work correctly if used in Code First mode. To continue using Database First or Model First ensure that the Entity Framework connection string is specified in
the config file of executing application. To use these classes, that were generated from Database First or Model First, with Code First add any additional configuration using attributes or the DbModelBuilder API and then remove the code that throws this exception.
In your opinion, what is the simplest way to fix this and achieve my goal?
I don't need to re-use the .edmx from the MVC project; or stick with code first.
I just want to add the neccessary tables from the MVC project to the database from the WinForms project, and make the MVC project be able to access the database.
Then
1. Ensure that you add tables
2. Make the database in the winforms project available to MVC ( the simplest way is to install the sql server and make there the database)
claudia888
for most pages from the MVC project, this is what I see:
Code generated using the T4 templates for Database First and Model First development may not work correctly if used in Code First mode. To continue using Database First or Model First ensure that the Entity Framework connection string is specified in
the config file of executing application. To use these classes, that were generated from Database First or Model First, with Code First add any additional configuration using attributes or the DbModelBuilder API and then remove the code that throws this exception.
Said alredy: That is because you did NOT reuse the Winforms connection string!
Let asssume that Winforms works correctly with database,
1. Extract from Winforms the edmx and put in a separate dll
2. reference from winforms the dll
3. ensure winforms works
4. reference from MVC the dll . compile. make small changes( maybe .AddObject versus .Add)
claudia888
Member
193 Points
485 Posts
An ASP.NET project with BOTH WinForms files and MVC3 files - can it work?
Aug 30, 2012 02:39 AM|LINK
Hi,
Has anyone had experience working on an ASP.NET project that contains both files developed using WinForms and files developed using MVC3?
In theory - can it work?
I have a deadline to make this work, but I'm seeing quite some errors now; below is an example. Note: the MVC3 files were developed seperately using EF4, database first, then auto-generate models and .edmx; and it worked well - now after combining them with some WinForms files into 1 project, it does not work.
Exception Details: System.Data.Entity.Infrastructure.UnintentionalCodeFirstException: Code generated using the T4 templates for Database First and Model First development may not work correctly if used in Code First mode. To continue using Database First or Model First ensure that the Entity Framework connection string is specified in the config file of executing application. To use these classes, that were generated from Database First or Model First, with Code First add any additional configuration using attributes or the DbModelBuilder API and then remove the code that throws this exception.
I searched aorund; here is an answer I found, on the exact same error I encounterd above:
</div>
Once you are using model-first / database-first (EDMX) you must use Entity connection string with referencing .ssdl, .msl and .csdl metadata files.
</div>The prpblem with my situation is: I need the code developed using MVC3 & EF4 to start using an existing database, which is closely tied to the code developed using WinForms - so I had to change the connectionstring in web.config file to point to the database; thus I get this error.
How to make this work?
Thanks,
Claudia
ignatandrei
All-Star
137716 Points
22159 Posts
Moderator
MVP
Re: An ASP.NET project with BOTH WinForms files and MVC3 files - can it work?
Aug 30, 2012 03:22 AM|LINK
Yes. See http://msprogrammer.serviciipeweb.ro/2010/03/29/asp-net-mvc-orm-and-viewmodels/
This is because your connection string is not in the edmx form - but in code first form.
1. Check your connection string in both winforms and mvc project.
2. Check also Code First generated code for UnintentionalCodeFirstException ( search in your solution) . Do not remove- insted , think how can you generated fluent api code( no template yet for this)
claudia888
Member
193 Points
485 Posts
Re: An ASP.NET project with BOTH WinForms files and MVC3 files - can it work?
Aug 30, 2012 04:34 AM|LINK
Hi Andrei,
re your answers:
This is because your connection string is not in the edmx form - but in code first form.
Yes - I figured that out; see my post.
1. Check your connection string in both winforms and mvc project.
What I need to do is: combine the 2 projects into 1 solution; and use 1 database. So the connectionstring from the original MVC project has to be changed into using the same connectionstring (that points to the actual db, not its original one with metadata) ... correct?
2. Check also Code First generated code for UnintentionalCodeFirstException ( search in your solution) . Do not remove- insted , think how can you generated fluent api code( no template yet for this)
By "api code"- are you suggesting that I should not try to combine these 2 projects into 1 solution, but rather keep them seperate and build an api in between them?
The original MVC project has its own .mdf - I'm struggling to somehow add its tables into the database for the WinForms project (which has a much larger database and lot more functionality); then in the web.config file for the MVC project, use the SAME connectionstring.
Is it possible to make this work? Am I on the right track, or is there a better way to approach this?
Thanks,
Claudia
ignatandrei
All-Star
137716 Points
22159 Posts
Moderator
MVP
Re: An ASP.NET project with BOTH WinForms files and MVC3 files - can it work?
Aug 30, 2012 05:16 AM|LINK
Both are needed to use same connectionstring - the one with metadata . If you want to use codefirst, you should search for UnintentionalCodeFirstException and modify the template .tt to generate relationships.
No - I mean http://msdn.microsoft.com/en-us/data/hh134698.aspx
More - I think you should use a common dll for edmx and database related stuff - like in http://msprogrammer.serviciipeweb.ro/2010/03/29/asp-net-mvc-orm-and-viewmodels/ - see how I use for a console and a MVC project.
First CONSOLIDATE the 2 databases -make have the same tables / columns. Then you can re-use edmx and stuff..
( other approach, much harder, is to use interfaces - and make call Repository that use database implementations to save data)
claudia888
Member
193 Points
485 Posts
Re: An ASP.NET project with BOTH WinForms files and MVC3 files - can it work?
Aug 30, 2012 03:18 PM|LINK
Hi Andrei,
Thanks for the clarification!
A quick question - re your statement "First CONSOLIDATE the 2 databases -make have the same tables / columns. Then you can re-use edmx and stuff..":
In my case, the 2 databases do NOT have the same tables/columns. The WinForms app was developed 4 years ago, using VS2005 and SQL server 2005; it has a lot of tables and features that I need to utilize and do not want to re-create; the MVC app was just developed in the last few months, using VWD2010 and SQL server express 2008 -it is a much smaller app, with less tables and logics.
My job is to combine these 2 projects; more specifically: "ADD" the MVC project into the WinForms project. So I did the following:
1) Upgrade the WinForms project's db to 2008 in SQL server express 2008 Management Studio - done, no error;
2) Convert the WinForms project's code in VWD2010, re-target ASP.NET4 framework - almost done; I can build the solution, hit F5 to bring the app interface up, most functions work correctly....1 error left during build that I'm still troubleshooting;
3) Add tables from the MVC project into the db for the WinForms project, define relationships - done;
4) Add the MVC project code as an additional project to the WinForms solution - done; I can compile the overall solution, and the MVC project does not give any error.
5) Make the MVC project code work with the "Additonal tables" that I added in step 3) in the overall DB - I'm stuck here.... I can bring the MVC project interface up but most pages give errors; I have edited the web.config file for the MVC project, to remove the connectionstring with the metadata, etc. based on the error messages I got so far - hence I posted this question.
So do you still think that it is possible for me to consolidate the 2 dbs into 1, based on the above info?
Please let me know - thanks so much for your help!
Claudia
claudia888
Member
193 Points
485 Posts
Re: An ASP.NET project with BOTH WinForms files and MVC3 files - can it work?
Aug 30, 2012 09:32 PM|LINK
Hi Andrei,
Update - I read the links you provided about Fluent API; plus read more on EF. 2 more questions:
1) Fluent API and Code First - it seems that the benefit of Fluent API is to add extra configuration capability for creating tables/DB, when using Code First; if I do NOT use code first moving forward, just adding/modifying tables "the old way", and develop my logic to work with the tables ... do I still need to worry about it?
Note: I do want to learn this and use it; but for the current project, I have a deadline and really just want to get things done as quickly as possible.
2) ViewModel - supposed I'm able to conslidate the 2 dbs into 1; can I define a ViewModel between a table from the "original" database (from the WinForms project), and a NEW table that I just added for the MVC project?
Thanks!
Claudia
ignatandrei
All-Star
137716 Points
22159 Posts
Moderator
MVP
Re: An ASP.NET project with BOTH WinForms files and MVC3 files - can it work?
Aug 31, 2012 01:45 AM|LINK
It's strange. It should work as long as there are SAME tables ... or did you integrate somehow with existing Winforms tables ?!
ignatandrei
All-Star
137716 Points
22159 Posts
Moderator
MVP
Re: An ASP.NET project with BOTH WinForms files and MVC3 files - can it work?
Aug 31, 2012 01:47 AM|LINK
Use the old way in MVC then - no need for code first
.
In the previous post you just did.
create a separate dll.
claudia888
Member
193 Points
485 Posts
Re: An ASP.NET project with BOTH WinForms files and MVC3 files - can it work?
Aug 31, 2012 02:52 AM|LINK
Hi Andrei,
You said: "First CONSOLIDATE the 2 databases -make have the same tables / columns. Then you can re-use edmx and stuff..":
In my case, the 2 databases do NOT have the same tables/columns.
So - is it possible for me to consolidate these 2 databases into 1?
I don't need to re-use the .edmx from the MVC project; or stick with code first.
I just want to add the neccessary tables from the MVC project to the database from the WinForms project, and make the MVC project be able to access the database.
Now, for most pages from the MVC project, this is what I see:
Code generated using the T4 templates for Database First and Model First development may not work correctly if used in Code First mode. To continue using Database First or Model First ensure that the Entity Framework connection string is specified in the config file of executing application. To use these classes, that were generated from Database First or Model First, with Code First add any additional configuration using attributes or the DbModelBuilder API and then remove the code that throws this exception.
In your opinion, what is the simplest way to fix this and achieve my goal?
Thanks,
Claudia
ignatandrei
All-Star
137716 Points
22159 Posts
Moderator
MVP
Re: An ASP.NET project with BOTH WinForms files and MVC3 files - can it work?
Aug 31, 2012 03:03 AM|LINK
Then
1. Ensure that you add tables
2. Make the database in the winforms project available to MVC ( the simplest way is to install the sql server and make there the database)
Said alredy: That is because you did NOT reuse the Winforms connection string!
Let asssume that Winforms works correctly with database,
1. Extract from Winforms the edmx and put in a separate dll
2. reference from winforms the dll
3. ensure winforms works
4. reference from MVC the dll . compile. make small changes( maybe .AddObject versus .Add)
6. Ensure MVC works.