Wednesday, 20 November 2013

SQL Server 2012 prevent save changes to table



When you design a table in a database and then try to make a change to a table structure that requires the table to be recreated, the management tools will not allow you to save the changes.
You will get an error stating, “You have either made changes to a table that can’t be re-created or enabled the option Prevent saving changes that require the table be re-created.”

This is caused when you make one of the following changes:
  • You change the Allow Nulls setting for a column.
  • You reorder columns in the table.
  • You change the column data type.
  • You add a new column.


Here’s how you fix it:
In Management Studio, go to Tools –> Options –> Designers –> Tables and Designers and uncheck the Prevent Saving Changes that require table re-creation option.


Tuesday, 19 November 2013

Error: throw new 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.






If the connection string has the metadata, EF thinks it is Model First or Database First. If it is a plain connection string, EF thinks it is Code First. However, if you want to start out doing model first but make EF think you are really doing code first (which is what you are doing), make sure you are using the DbContext code generator, not the default one. Code first POCOs are really that--"plain old c# objects"-- no special database aware or change tracking stuff in them at all. To use the DbContext code generator, right click on your model diagram and choose "Add new code generation item..." then select the ADO.NET DbContext Generator. Also, depending on how you named your primary and foreign keys and/or whether they are more complicated than just simple int IDs, you will probably need to fill in some code to map the relationships between your objects in the "OnModelCreating" method in your context. Delete the line throw new UnintendedCodeFirstException(); and replace it with your mapping code. Otherwise EF may not be able to figure out all the relationships (remember there's no metadata for it to rely on).

In my case: I's working on a test project and my database was Northwind DB. I had 3 layer in my project Main project, DAL, BLL. In my executing project connection string was:

<add name="NorthwindDBEntities" providerName="System.Data.SqlClient" connectionString="data source=(local);initial catalog=NorthwindDB;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework" />

So, encountered error!!!
And then, change it to following:

<add name="NorthwindDBEntities" connectionString="metadata=res://*/Northwind.csdl|res://*/Northwind.ssdl|res://*/Northwind.msl;provider=System.Data.SqlClient;provider connection string=&quot;data source=(local);initial catalog=NorthwindDB;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework&quot;" providerName="System.Data.EntityClient" />


Look, here I've included the metadata information with along connection string.
Hope this will help :)


Credit goest to:
http://stackoverflow.com/users/10263/brian-rogers

Visual Studio 2012 compatibility issue



A flaw in visual studio may cause it to become unstable when the .NET 4.5 framework is updated.


When you update your windows 7/8 or .NET framework you may face this problem. This problem says that VS 2012 has compatibility issue. You don't want to see this every time you start VS 2012, right?

Solution:
Download the patch from following location and install. Hope you find your solution :)

http://www.microsoft.com/en-us/download/details.aspx?id=36020

Compile c sharp (.cs) file using command prompt



When I started Google about how to compile C# (.cs) file I found a lot of tutorial on that. Of course many of those article are correct I found some difficulty to run my single C# (.cs) file.



Why I need to compile separately:
Sometimes we found many tutorial or small code snippet on various desired topic, but to run that from Visual Studio we have to create Project and have to some unnecessary mouse click. So I tried to run the file directly from command prompt. Now lets dive into the process:


  1. Hope you must have installed Visual Studio in your computer.
  2. Open Windows command prompt.
  3. Go to the directory of Visual Studio bin folder. Example: C:\Program Files\Microsoft Visual Studio 10.0\VC\bin
  4. Then run the BATCH file >> vcvars32.bat Example: C:\Program Files\Microsoft Visual Studio 10.0\VC\bin\vcvars32.bat. Now environment is set to run/compile your c sharp file.
  5. csc.exe this is the file that compile your .cs file and make the .exe file in the bin directory. your .cs file can be placed anywhere in your computer.
  6. Write csc following a space and then write your filename.cs and press enter. Example: C:\Program Files\Microsoft Visual Studio 10.0\VC\bin\csc filename.cs
  7. After compiling successfully the .exe file has been created in your bin folder.
  8. Now write the filename without .cs and it will run.