I am practicing Entity Framework. I am practicing Asp.net MVC framework using Razor Engine and EF Code First. In practicing it, I found to have few problems. The problem is the Entity Framework doesnt creates a database in my SQL.
I am using VisualStudio 2011 Beta Version with MSSQL 2008. I dont know what the problem is.
Help required. Below is my Code :
Person.cs
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Text;
namespace MvcDemo.Data
{
public class Person
{
[Key]
public int PersonID { get; set; }
[Required]
[StringLength(40)]
public string FirstName { get; set; }
[Required]
[StringLength(30)]
public int LastName { get; set; }
public int? Age { get; set; }
[ForeignKey("PrefixID")]
public Prefix Prefix { get; set; }
[Required]
public int PrefixID { get; set; }
}
}
Prefix.cs
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Text;
namespace MvcDemo.Data
{
public class Prefix
{
public int PrefixID { get; set; }
[Required]
[StringLength(20)]
public string PrefixName { get; set; }
}
}
MvcContext.cs
using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Text;
using MvcDemo.Data;
namespace MvcDemo.DataAccess
{
public class MvcContext : DbContext
{
public MvcContext()
: base("name = MvcDemoApp")
{
}
public DbSet<Person> People { get; set; }
public DbSet<MvcDemo.Data.Prefix> Prefix { get; set; }
}
}
ConnectionString:
<connectionStrings>
<add name="MvcDemoApp"
connectionString="Server=.;Initial Catalog=MvcDemoApp;Integrated Security=true"
providerName="System.Data.SqlClient" />
If this is all of your code it is not surprising that a database is not created. There is no code that executes commands against the database. As soon as you start iterating over e.g. MvcContext.People or do some insert you will notice that the database gets created.
There are plenty step-by-step examples online. I would pick one of them and do a walk through; you can try this one for example: http://msdn.microsoft.com/en-us/data/gg685467.aspx. It should cover most of the basics to get you started.
If you prefer video over text, search Channel 9, or pluralsight
You should operate package manager console to do it.
Ope package manager console window and run "update-database" command, there still a bug in this command so you need to specify "startupprojectname" param to execute it and connection string in you DbContext file.
Related
I have an model I reference in a DbContext class I'm using to generate my code-first DB. Originally the model was named FeedEventDomainModel and I changed the name to FeedEventCommand.I haven't generated the DB yet; however when I run the application to open Index.html under the areas folder, I receive the following error:
Code:
DbContext Class
using System.Data.Entity;
namespace OProj.DataContext
{
public class OProjDBContext : DbContext
{
public OProjDBContext() : base("name=OProjDB")
{
}
public DbSet<FeedEventCommand> FeedEvents { get; set; }
}
}
FeedEventCommand
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Text;
namespace OProj.DataContext
{
public class FeedEventCommand
{
[Key]
public int Id { get; set; }
public int AnimalId { get; set; }
public int FeederTypeId { get; set; }
public string FeederType { get; set; }
}
}
My question is since I changed the name of my model from FeedEventDomainModel to FeedEventCommand, is there a place in cache I need to clear so it doesn't keep referencing the old model name?
do a full clean.
close Visual Studio
delete the temp files in Windows/Microsoft.Net/Framework and Framework64/v4.0/Temporary ASP.NET Files
reopen the solution in VS, rebuild and it should work.
renaming things has that kind of effect so you have to delete the cached files
I am new to MVC and i am having a problem. I have one table named tblEmployee , model Employee. Controller EmployeeController. i am having EntityException in my EmployeeController controller.
My code for Model:
namespace MvcPractice.Models
{
[Table("tblEmployee")]
public class Employee
{
public int EmployeeId { get; set; }
public string Name { get; set; }
public string Gender { get; set; }
public string City { get; set; }
}
}
i have created EmployeeContext.cs in Model:
using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Web;
namespace MvcPractice.Models
{
public class EmployeeContext : DbContext
{
public DbSet<Employee> Employees { get; set; }
}
}
Connection string:
<connectionStrings>
<add name="EmployeeContext"
connectionString="server=.;database=MvcDemo; integrated security=SSPI;"
providerName="System.Data.SqlClient"/>
</connectionStrings>
This is a web application but you are using integrated security to connect. That will use the identity of your web application. It's preferable to use a real SQL Server user created for your application. So create a user and change your connection string to something like this:
server=.;database=MvcDemo;User ID=<username>;Password=<password>;
Additionally, it's unusual for a local installation of SQL Server to be installed with an empty instance name, so you may also need to check that the server name shouldn't be this, just check what your SQL Server Management Studio is using to connect:
.\sqlexpress
I am trying to make an application where in I have made a Location model,controller and views using scaffolding. I have location name and location ID properties in location. Database is getting created and location table also and data is getting populated. Now i want to make a department class where I have 3 properties namely Dept_ID, Dept_Name and Loc_ID(Foreign key). I have added required codes in respective files as following.
in Department.cs(Model)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel.DataAnnotations;
namespace DMS.Models
{
public class Department
{
public int D_ID { get; set; }
public string D_Name { get; set; }
[ForeignKey("L_ID")]
public int L_ID { get; set; }
}
}
in DB_CONTEXT class
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.Entity;
namespace DMS.Models
{
public class DB_CONTEXT : DbContext
{
public DbSet<Location> Movies { get; set; }
public DbSet<Department> Department { get; set; }
}
}
and in locatoin.cs(Model)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace DMS.Models
{
public class Location
{
public int ID { get; set; }
public string L_Name { get; set; }
}
}
when i am trying to add a controller for Department I am getting an error as
unable to retrieve metadata for 'DMS.Models.Department' The navigation property 'L_ID' is not declared property on type Department.Verify that it has not been explicitly excluded from the model and that it is a valid navigation property.
[ForeignKey("LoactionID")]
public int L_ID { get; set; }
public Virtual Location Location {get;set;}
Make these changes in Department model and try once again. I hope this will solve your issue.
I'm working on an EF Code first site, and I've written my classes and a context class, the source of which is:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Entity;
using MySite.SalesTool.Data.Entities;
using System.Data.Entity.ModelConfiguration.Conventions;
namespace MySite.SalesTool.Data
{
public class SalesToolEntities : DbContext
{
public DbSet<User> Users { get; set; }
public DbSet<UserRole> UserRoles { get; set; }
public DbSet<Job> Jobs { get; set; }
public DbSet<JobAssigner> JobAssigners { get; set; }
public DbSet<JobFile> JobFiles { get; set; }
public DbSet<JobStatus> JobStatuses { get; set; }
public DbSet<AssignedUser> AssignedUsers { get; set; }
}
}
The project builds fine, but when I go to run the site, no tables are created in the database and I get an error stating that the database can't find whichever context object I try and access, presumably because the code first has not generated any of the necessary tables.
Any ideas why it wouldn't generate any of the tables at all, and not give me any kind of error information?
Do you have an initialization strategy?
Database.SetInitializer(new DropCreateDatabaseIfModelChanges<SalesToolEntities>());
From what you subscribe it sounds like you've created the database yourself. Then you need to specify an initialization
strategy otherwise no tables/data will be added to the database and querying the database will result in an exception: {"The specified table does not exist. [ sometable ]"}
I'm using code-first in Web Forms (not MVC) with EF4 and CTP5, and when trying to decorate a property with the [Key] attribute, it doesn't show up in the intellisense and get compilation error saying KeyAttribute was not found. Here is my code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel.DataAnnotations;
namespace ERP.Models
{
public class CustomerAddress
{
[Key]
public int AddressID { get; set; }
public int CustomerID { get; set; }
public string Address1 { get; set; }
public string Address2 { get; set; }
public int CityID { get; set; }
public int SateID { get; set; }
}
}
I have included the DataAnnotations library, all looks ok, but the [Key] attribute is not found. Any hint is highly appreciated. Thanks!
I didn't post it as answer first because I wasn't sure if the problem is not elsewhere. You must add System.ComponentModel.DataAnnotations.dll to project references.