ADO.NET, Entity Framework, LINQ to SQL, NHibernatehttp://forums.asp.net/1227.aspx/1?ADO+NET+Entity+Framework+LINQ+to+SQL+NHibernateUsing ADO.NET, Entity Framework, LINQ to SQL, NHibernate, and other ORMs (Object-Relational Mapping) with ASP.NETWed, 22 May 2013 19:42:11 -0400urn:uuid:00000000-0000-0000-0000-000000001227urn:uuid:00000000-0000-0000-0000-000005402667http://forums.asp.net/p/1908906/5402667.aspx/1?ADO+NET+Data+Model+I+m+getting+NullReferenceExceptionADO.NET Data Model..... I’m getting NullReferenceException <p>I'm having trouble with a simple ADO.NET Data Model… Nothing hard but don't get it working&nbsp;</p> <p>What I have:</p> <p>A Website project&nbsp; with &nbsp;entity model (.edmx) named EMSDataSystemsModel&nbsp; and ObjectContext called EMS_ObjectContext</p> <p>I’m trying to Loop through and Insert XML data in the local ObjectContext data as follows:</p> <p>EMSDataSystemsModel.EMS_DBEntities EMS_ObjectContext = new EMSDataSystemsModel.EMS_DBEntities(System.Web.Configuration.WebConfigurationManager.ConnectionStrings[&quot;EMS_DBEntities&quot;].ConnectionString);</p> <p>foreach (XElement filteredCountry in countries_WithPhNoFormat)</p> <p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {</p> <p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // Add the new country to the Countries collection</p> <p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; EMS_ObjectContext.Countries.AddObject(new EMSDataSystemsModel.Country</p> <p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {</p> <p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; CountryID = new Guid(filteredCountry.Attribute(&quot;ID&quot;).Value),</p> <p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Name = filteredCountry.Attribute(&quot;Name&quot;).Value,</p> <p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; PhoneNoFormat = filteredCountry.Attribute(&quot;PhoneNoFormat&quot;).Value,</p> <p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;DialingCountryCode =&nbsp;&nbsp;&nbsp;</p> <p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;filteredCountry.Attribute(&quot;DialingCountryCode&quot;).Value,</p> <p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; InternationalDialingCode = &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</p> <p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; filteredCountry.Attribute(&quot;InternationalDialingCode&quot;).Value,</p> <p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; InternetTLD = filteredCountry.Attribute(&quot;InternetTLD&quot;).Value</p> <p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; });</p> <p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }</p> <p>I’m getting NullReferenceException.</p> <p>I debugged it but couldn’t find a null object. I 'm stuck with it, anyone any&nbsp;clues??</p> <p>I appreciate your help in advance.</p> <p>Thanks&nbsp;</p> 2013-05-22T21:17:16-04:002013-05-22T21:17:16.57-04:00urn:uuid:00000000-0000-0000-0000-000005401111http://forums.asp.net/p/1908513/5401111.aspx/1?If+clause+for+where+clause+in+entitydata+sourceIf clause for where clause in entitydata source <p>I have a gridview populated under conditions established in the where clause. Is it possible to add some if condition? For example: if inserted value is equal to 0 don't filter</p> 2013-05-21T14:09:01-04:002013-05-21T14:09:01.597-04:00urn:uuid:00000000-0000-0000-0000-000005402703http://forums.asp.net/p/1908913/5402703.aspx/1?why+are+ienumberable+updated+values+lost+why are ienumberable<> updated values lost? <p>I need to update some field values after I query the database, but before i pass the results to my view.&nbsp; I'm doing the following, but I was surprised to find my updated values are lost, i.e. the result set that is passed to the view contains the original values BEFORE I modified them!&nbsp; This is display-only data so I don't care about binding, or updating the database, etc. I just want my updated values to be passed to the view.</p> <p>&nbsp;</p> <pre class="prettyprint">var q= (from a in db.table1 join b in db.table2 on a.id = b.id select new MyData() { CId = a.CId, CNo = b.CNo, Name = b.Name, Desc = b.Desc, Email = a.Login }); foreach (MyData r in q) { //when I step through the code, I can see the value being updated r.Email = process(r.Email); } //if I check the values here, they have reverted to what they were before the update! return qry; The view also shows the original values.</pre> <p></p> 2013-05-22T23:19:29-04:002013-05-22T23:19:29.153-04:00urn:uuid:00000000-0000-0000-0000-000005402623http://forums.asp.net/p/1908891/5402623.aspx/1?Map+a+property+to+a+different+entity+on+a+condition+Entity+Framework+4+5Map a property to a different entity on a condition Entity Framework 4/5 <p>Hi,</p> <p>I am working on a database first model. I have an additional property in one of the entity which is not present in the table. Its value should be derived at runtime based on values in other entity.&nbsp;</p> <p>Something similar to following:</p> <p>MyEntity.MyProperty = (SomeEntity1.SomeProp !=null) <strong>?</strong>&nbsp;SomeEntity1.SomeProp<strong> :</strong>&nbsp;SomeEntity2.SomeProp</p> <p>I am aware of the couple of alternative solutions like using stored procedure/views and LINQ etc, but I wanted to know if there is anything already provided by EF.</p> 2013-05-22T19:44:12-04:002013-05-22T19:44:12.01-04:00urn:uuid:00000000-0000-0000-0000-000005402430http://forums.asp.net/p/1908843/5402430.aspx/1?Maddening+Underlying+provider+failed+on+open+error+Maddening "Underlying provider failed on open" error. <p>Ok, here is the situation :</p> <p>Have a WCF service that uses entity framework 4.1 to retrieve and send data to SQL 2008 database. Service runs fine on my PC. Added the code to Visual Source Safe, another dev brought the code down to her machine, built fine, deployed to her local IIS, can access the service, but every time we try to run any actual call to the db via EF, like this for example :</p> <p></p> <pre class="prettyprint">using (var db = new VideoLibraryEntities()) { var grps = db.VideoGroups.ToList(); foreach (var g in grps) { VideoGroupMdl mdl = new VideoGroupMdl();</pre> <p>We get this error:</p> <p></p> <p>The underlying provider failed on Open.</p> <p>Server stack trace:</p> <p>&nbsp;&nbsp;&nbsp;at System.ServiceModel.Channels.ServiceChannel.ThrowIfFaultUnderstood(Message reply, MessageFault fault, String action, MessageVersion version, FaultConverter faultConverter)</p> <p>&nbsp;&nbsp; at System.ServiceModel.Channels.ServiceChannel.HandleReply(ProxyOperationRuntime operation, ProxyRpc&amp; rpc)</p> <p>&nbsp;&nbsp; at System.ServiceModel.Channels.ServiceChannel.Call(String action, Boolean oneway, ProxyOperationRuntime operation, Object[] ins, Object[] outs, TimeSpan timeout)</p> <p>&nbsp;&nbsp; at System.ServiceModel.Channels.ServiceChannelProxy.InvokeService(IMethodCallMessage methodCall, ProxyOperationRuntime operation)</p> <p>&nbsp;&nbsp; at System.ServiceModel.Channels.ServiceChannelProxy.Invoke(IMessage message)</p> <p>&nbsp;</p> <p>Exception rethrown at [0]:</p> <p>&nbsp;&nbsp;&nbsp;at System.Runtime.Remoting.Proxies.RealProxy.HandleReturnMessage(IMessage reqMsg, IMessage retMsg)</p> <p>&nbsp;&nbsp; at System.Runtime.Remoting.Proxies.RealProxy.PrivateInvoke(MessageData&amp; msgData, Int32 type)</p> <p>&nbsp;&nbsp; at IVideoLibraryService.GetAllGroups()</p> <p>&nbsp;&nbsp; at VideoLibraryServiceClient.GetAllGroups()</p> <p></p> <p>Ugh! We have verified that the username and pwd in the connection string is good, and when in Visual Studio, we can browse the model browser just fine after clicking on the edmx file. We have looked through everything we can think of, and just can't get it to work. I am thinking it has to be environmental, as the SAME EXACT code runs fine on my PC.</p> <p>Does anyone have any ideas, thoughts or suggestions of things to check? We are desperate! Thank you.</p> <p></p> 2013-05-22T15:57:42-04:002013-05-22T15:57:42.813-04:00urn:uuid:00000000-0000-0000-0000-000005402209http://forums.asp.net/p/1908797/5402209.aspx/1?Help+translating+SQL+to+LinqHelp translating SQL to Linq <p></p> <p>I'm trying to translate this (My)SQL into Linq.</p> <pre class="prettyprint">SELECT c.courseId, GROUP_CONCAT(CONCAT_WS('_', u.userId, s.firstName, s.lastName) SEPARATOR ',') AS Students FROM ( SELECT c.courseId FROM Courses c WHERE c.EndDate = '2012-10-20' ) AS c JOIN StudentCourses sc ON c.courseId = sc.courseId JOIN Students s ON sc.studentId = s.studentId JOIN Users u ON sc.studentId = u.studentId GROUP BY c.courseId</pre> <pre class="prettyprint">courseId | Students ------------------- 1 | ab0001_Adam_Biggle,st0321_Steve_Temerick,ca2112_Charlie_Anderson 2 | dk4321_Dan_King 3 | </pre> <p></p> <p>In code I want to populate Course objects</p> <pre class="prettyprint">class Course { int CourseId {get; set;} List&lt;Student&gt; { get; set;} } class Student { string UserId { get; set;} string FirstName {get; set;} string LastName {get; set;} }</pre> <p>How can I go about doing this with Linq (entityframework)?<br> My attempts have resulted in very slow queries.</p> <p></p> 2013-05-22T12:38:48-04:002013-05-22T12:38:48.163-04:00urn:uuid:00000000-0000-0000-0000-000005401892http://forums.asp.net/p/1908719/5401892.aspx/1?Query+about+Entity+FrameworkQuery about Entity Framework <p>i never work with &nbsp;Entity Framework but i heard the term called database first, model first, code first. can anyone tell me what it is all about?</p> <p>what is the meaning of database first ?</p> <p>what is the meaning of code first ?</p> <p>what is the meaning of model first ?</p> <p>also guide with few article which will discuss all above in depth. help me to understand &amp; work with database first, model first, code first approach for beginner. thanks</p> 2013-05-22T08:04:37-04:002013-05-22T08:04:37.89-04:00urn:uuid:00000000-0000-0000-0000-000005402107http://forums.asp.net/p/1908770/5402107.aspx/1?How+to+execute+store+procedure+from+JavaScript+function+How to execute store procedure from JavaScript function? <p>Hi All,</p> <p>I have to execute store procedure which has 3 parameters on button click.</p> <p>On button click &nbsp;ihave written javascript for some activity.</p> <p>Now i want to execute my store procedure from function.</p> <p>Working on MVC 3</p> 2013-05-22T11:04:20-04:002013-05-22T11:04:20.267-04:00urn:uuid:00000000-0000-0000-0000-000005401996http://forums.asp.net/p/1908741/5401996.aspx/1?open+excel+document+using+asp+net+web+page+read+and+upload+data+to+sql+server+open excel document using asp.net web page, read and upload data to sql server. <p>hi</p> <p>i have a web page built with asp.net and c# which opens an excel document and reads its data and save it in sql server. when i debug this the code works fine and gives me the desired results.&nbsp;</p> <p>when i publish my application and browse the said page, it gives me the following error.&nbsp;</p> <h2><i>Retrieving the COM class factory for component with CLSID {00024500-0000-0000-C000-000000000046} failed due to the following error: 80070005 Access is denied. (Exception from HRESULT: 0x80070005 (E_ACCESSDENIED)).</i></h2> <p></p> <p>could the problem be not having this driver (<strong>&nbsp;</strong><a href="http://www.microsoft.com/downloads/details.aspx?FamilyID=7554F536-8C28-4598-9B72-EF94E038C891&amp;displaylang=en" target="_blank"><strong>2007 Office System Driver : Data Connectivity Components</strong></a>) installed in my server?</p> <p></p> 2013-05-22T09:29:33-04:002013-05-22T09:29:33.05-04:00urn:uuid:00000000-0000-0000-0000-000005395204http://forums.asp.net/p/1906331/5395204.aspx/1?How+to+save+XML+in+SQL+Server+2005+table+column+using+Entity+Framework+4+1How to save XML in SQL Server 2005 table column using Entity Framework 4.1 <p>I have to do these 2 things</p> <ol> <li> <p>I have an object I converted that to XML now I want to save that XML into a SQL Server 2005 column of type&nbsp;<pre class="prettyprint">XML</pre></p> </li><li> <p>then I want to retrieve that XML and convert it back to an object.</p> </li></ol> <p>I am using Entity Framework 4.1 code-first POCOS</p> <p>Please tell me solution.</p> <p>Thanks</p> 2013-05-15T21:27:52-04:002013-05-15T21:27:52.453-04:00urn:uuid:00000000-0000-0000-0000-000005401822http://forums.asp.net/p/1908707/5401822.aspx/1?DataTable+problemDataTable problem <p>I have this:</p> <pre class="prettyprint">DataTable dtcortes = new DataTable(); dtcortes.Columns.Add(&quot;id&quot;,typeof(string)); dtcortes.Columns.Add(&quot;value&quot;, typeof(Int32));</pre> <p>i need to test if the id 1 exists.</p> <p>if not exists to add to DataTable if exists add 1 to collumn value</p> <p>Thank's in advance</p> <p></p> <p>Marco<br> <br> </p> 2013-05-22T07:12:41-04:002013-05-22T07:12:41.85-04:00urn:uuid:00000000-0000-0000-0000-000005309033http://forums.asp.net/p/1884291/5309033.aspx/1?MVC4+Enity+relationships+and+how+to+programMVC4 Enity relationships and how to program <p>Hi,</p> <p>I'm making an online appointment website. It has 4 entities: Clients, Appointments, Staff and Appointment Types.</p> <p>Client and Staff have a 1 to many relationship with Appointment.</p> <p>AppointmentType also has a 1 to many relationship with Appointment.</p> <p>The problem is that I'm using inheritance to have Clients and Staff store thier data in one table. This means they have the same primary key (PersonID) and thier data is stored in the same table. I want Appointment to have seperate IDs for clients and staff so you can tell who's got an appointment with which staff member.</p> <p>Does anyone have an idea of what I should do? I've gone over the entity framework tutorials, MVC tute and MVC music store (though that was a while ago) but no idea yet what to do.</p> 2013-02-21T02:01:23-05:002013-02-21T02:01:23.07-05:00urn:uuid:00000000-0000-0000-0000-000005400587http://forums.asp.net/p/1908410/5400587.aspx/1?+nHibernate+Class+Is+not+mapped“nHibernate” [Class] Is not mapped <p><span>I have an asp.net application using nhibernate for data accessing. Application works fine for a month. After that it stops working , Application error log Says &quot;User is not mapped [from User a where upper(UserName)='Tom']. Whenever i receive this error, I just restart the iis and then application will start to work without any issues. I could not identify the actual issue. Please help me to solve this issue.</span></p> 2013-05-21T07:38:08-04:002013-05-21T07:38:08.05-04:00urn:uuid:00000000-0000-0000-0000-000004987231http://forums.asp.net/p/1805141/4987231.aspx/1?How+to+update+record+in+a+table+using+LINQ+How to update record in a table using LINQ? <p>Hi all,</p> <p>I am new to Linq and want to update data in a table. How I can do??? I create datacontext also and values assign to it as well and in the last dc.SubmitChanges(); also code but updation does not perform. I test for insertion with this data context its ok.</p> <p>Atiq</p> <p></p> 2012-05-18T13:54:00-04:002012-05-18T13:54:00.51-04:00urn:uuid:00000000-0000-0000-0000-000005392348http://forums.asp.net/p/1905646/5392348.aspx/1?EF+on+WebMatrix+2+destroys+publishing+functionalityEF on WebMatrix 2 destroys publishing functionality &lt;div&gt; <p>A while back I renamed the db for my site from StarterSite.sdf to ThisREV5.sdf, shortly thereafter I added Entity Framework. Just now got around to publishign my site.</p> <p>Wihe I attempt to publish to Azure (this has worked flawlessly for a long time, I get a warning about &quot;only absolute paths being supported&quot;</p> <p>I did a blanket replace of StarterSite.sdf to ThisREV5.sdf in Visual Studio for the entire project. No references were therefore modified.</p> <p>The link contained in the log file is of no help.</p> <p>I am publishing to Azure.</p> <p>EDIT:</p> <p>I have since reverted back to StarterSite as the name of my db.&nbsp;</p> <p>Then I had to create a new site on Azure, and copy my local site to a new folder and got rid of EF.</p> <p>Was able to publish my site.</p> <p>So, I added EF back in and now I can no longer publish updates to my site.</p> <p>Copied my site to a new folder, created a new site on Azure, and I cannot publish. Same error message as below.</p> <p>Is Entity Frame mucking things up?</p> <p>5/12/2013 2:19 PM<br> 2:19:49 PM: Unable to automatically publish framework files. Your remote site may not work correctly.<br> ERROR: Could not publish to the remote database. Please ensure the publish profile contains a database and verify that the remote database is accessible.<br> 2:19:49 PM: Could not publish to the remote database. Please ensure the publish profile contains a database and verify that the remote database is accessible.<br> 2:19:49 PM: Could not publish the site. Could not publish to the remote database. Please ensure the publish profile contains a database and verify that the remote database is accessible.<br> 2:19:49 PM: Error detail:<br> 2:19:49 PM: File system path 'testthisREV/App_Data\ThisREV5.sdf' is not valid. Only absolute paths are supported.&nbsp; Learn more at: http://go.microsoft.com/fwlink/?LinkId=221672#ERROR_CANNOT_CONNECT_TO_DATABASE.<br> 2:19:49 PM:&nbsp;&nbsp;&nbsp; at Microsoft.Web.Deployment.SqlServerCeDatabaseProvider.GetAttributes(DeploymentAddAttributeContext addContext)<br> 2:19:49 PM:&nbsp;&nbsp;&nbsp; at Microsoft.Web.Deployment.DeploymentObject.EnsureValidAttributes()<br> 2:19:49 PM:&nbsp;&nbsp;&nbsp; at Microsoft.Web.Deployment.ManifestProvider.GetAttributes(DeploymentAddAttributeContext addContext)<br> 2:19:49 PM:&nbsp;&nbsp;&nbsp; at Microsoft.Web.Deployment.DeploymentObject.EnsureValidAttributes()<br> 2:19:49 PM:&nbsp;&nbsp;&nbsp; at Microsoft.Web.Deployment.DeploymentObject.get_IsValid()<br> 2:19:49 PM:&nbsp;&nbsp;&nbsp; at Microsoft.Web.Deployment.DeploymentManager.CreateObjectPrivate(DeploymentProviderContext providerContext, DeploymentBaseOptions baseOptions, DeploymentObject sourceObject, String serverVersion)<br> 2:19:49 PM:&nbsp;&nbsp;&nbsp; at Microsoft.Web.Deployment.DeploymentManager.CreateObject(DeploymentProviderOptions providerOptions, DeploymentBaseOptions baseOptions)<br> 2:19:49 PM:&nbsp;&nbsp;&nbsp; at Microsoft.Web.Deployment.Store.ApplicationDefinitionManager.CreateObject(String path, DeploymentBaseOptions baseOptions, String remoteComputername)<br> 2:19:49 PM:&nbsp;&nbsp;&nbsp; at Microsoft.WebMatrix.Deployment.WebDeployWorker.Execute(CommandManager commandManager, CertificateValidationService certificateValidationService, Boolean skipHiddenFiles)</p> &lt;/div&gt; 2013-05-13T12:29:48-04:002013-05-13T12:29:48.02-04:00urn:uuid:00000000-0000-0000-0000-000005401002http://forums.asp.net/p/1908495/5401002.aspx/1?Selecting+rows+in+Dataset+based+on+Filter+Criteria+in+C+Selecting rows in Dataset based on Filter Criteria in C# <p>Hi,</p> <p>I have one dataset. In this dataset I have some value. Now i want to filter the dataset based on the condition. Please suggest me.</p> <p>This is My code below :</p> <p>ds = cPhotoGallary.getAllPhotos()</p> <p><strong>Dim iddd As String = HttpUtility.HtmlEncode(Request.QueryString(&quot;BlogPostSID&quot;))</strong><br> <strong>Dim newiddd As Integer = Convert.ToInt32(iddd)</strong></p> <p><strong>&nbsp;dsdestination = ds.Table(0).[Select](newiddd) &nbsp; &nbsp; // But here i am getting Error.</strong></p> <p></p> 2013-05-21T12:29:50-04:002013-05-21T12:29:50.033-04:00urn:uuid:00000000-0000-0000-0000-000005395188http://forums.asp.net/p/1906324/5395188.aspx/1?+Retrieve+Stored+Procedure+Output+Parameters+in+EntityFramework Retrieve Stored Procedure Output Parameters in EntityFramework <pre class="prettyprint">System.Data.Objects.ObjectParameter Param = new System.Data.Objects.ObjectParameter(&quot;Message&quot;, typeof(int)); ctx.sp_ViRequest_Ins(Name, Email, Param); lblResult.Text = Param.Value.ToString();</pre> <p>My Stored procedure parameter</p> <p>@Name</p> <p>@Email</p> <p>@Message int output</p> <p>-------------------</p> <p>Error is displayed</p> <p>&nbsp;Argument 8: cannot convert from 'System.Data.Objects.ObjectParameter' to 'ref int?'&nbsp;</p> <p>how to fix it!</p> 2013-05-15T20:57:07-04:002013-05-15T20:57:07.65-04:00urn:uuid:00000000-0000-0000-0000-000005399486http://forums.asp.net/p/1908160/5399486.aspx/1?How+to+insert+uniqueidentifier+in+databse+filed+How to insert uniqueidentifier in databse filed <p>I have table called .Net_mem_biography&nbsp;include column name&nbsp;UserId&nbsp;of type&nbsp;uniqueidentifier</p> <p>i get the current session userId which comes from ASP.NET Membership proivder databse.</p> <p></p> <p>the code is&nbsp;</p> <p>string cu=Membership.GetUser().ProviderUserKey.ToString();<br> var bi = from cs in ctx.Net_mem_biography<br> where cs.UserId == cu<br> select cs;</p> <p>the error is&nbsp;</p> <p><strong>&quot;Operation == can't be applied to &nbsp;System.Guid and string&quot;</strong></p> <p></p> 2013-05-20T09:13:29-04:002013-05-20T09:13:29.73-04:00urn:uuid:00000000-0000-0000-0000-000005399549http://forums.asp.net/p/1908176/5399549.aspx/1?What+is+the+logic+behind+how+code+first+creates+a+DB+What is the logic behind how "code first" creates a DB? <p>Hi Everyone,</p> <p>I'm trying to create a rather simple DB using &quot;code first&quot; and I'm confused about a few issues on how the DB is created from the code.&nbsp;</p> <p>The DB should include the following 3 tables:</p> <p>ProductCategories</p> <p>ProductSubCategories</p> <p>Products</p> <p>Each item in ProductCategories has a List of Products (which are products with no sub-category), and similarly each item in ProductSubCategories has a list of Products (which are products with both a category and a sub-category).</p> <p>When the user is editting an item in Products he should be able to change both the category and the sub-category, so I thought I need in each item in Product a reference to both i's Category and SubCategory (if it has a sub category and null otherwise).</p> <p>I've looked into several examples and I see a few things that confuse me:</p> <p>- I noticed that when referencing a navigation&nbsp;object there's a need for both an Id field as well as a field containing the object iself.&nbsp;&nbsp;For example, I understand that in ProductSubCategories I need to a ProductCategory property as well as a ProductCategoryId property (and similarly also in Product I need these two as well as both ProductCategoryID and ProductCategory).&nbsp; Does that has to do with how MVC maps the information to the DB?&nbsp; what is the logic behind it?</p> <p>- I see that some items should be Virtual in the examples, and I'm tyring to figure the logic here too.&nbsp; I understand what &quot;vitual&quot; implies programmatically in C#&nbsp;- but why does a property of any kind needs to be virtual in this specific context of creating the DB?</p> <p>&nbsp;</p> <p>Thanks so much to all who help !</p> <p>Elior.</p> 2013-05-20T10:00:47-04:002013-05-20T10:00:47.727-04:00urn:uuid:00000000-0000-0000-0000-000005400320http://forums.asp.net/p/1908346/5400320.aspx/1?Map+MVC3+model+with+MSSQL+2008+view+using+both+code+first+and+database+first+techniqueMap MVC3 model with MSSQL 2008 view using both code first and database first technique <p>In SQL Server 2008 I have a view like this: &nbsp;&nbsp;</p> <pre class="prettyprint">CREATE VIEW products AS SELECT a.ID, a.Name, b.ID as SubID, b.Name as SubName FROM main_product as a INNER JOIN sub_product as b on a.ID = b.mainID</pre> <p>This is my model: &nbsp;&nbsp;</p> <pre class="prettyprint">Public class Products { public int ID { get; set; } public int Name { get; set; } public int SubID { get; set; } public int SubName { get; set; } }</pre> <p>Now how can I map my MVC3's model to the SQL Server view?<br /><br />I tried with this: <br /><br />&nbsp;&nbsp;</p> <pre class="prettyprint"> public class PartialContext : DbContext { protected override void OnModelCreating(DbModelBuilder modelBuilder) { modelBuilder.Conventions.Remove&lt;PluralizingTableNameConvention&gt;(); } public DbSet&lt;Test&gt; Test { get; set; } public DbSet&lt;Products&gt; Products { get; set; } } </pre> <p>and Controller: &nbsp;</p> <pre class="prettyprint"> public class ViewController : Controller { private PartialContext db = new PartialContext(); // // GET: /View/ public ViewResult Index() { return View(db.Products.ToList()); } }</pre> <p>This effort gave me an error: &quot;Invalid object name 'dbo.Products'.&quot;<br> <br> &nbsp;I want to use both `code first` and `database first` technique. It means I want to create both database and model class manually, not through code generator or database generator</p> 2013-05-21T01:53:01-04:002013-05-21T01:53:01.12-04:00