MVC 4 code first database initializer doesn't work - entity-framework

I had to stop at the same stages following different MVC 4 code first technique tutorials, because database initialization failed.
Using the connection
<add name="DefaultConnection" connectionString="Data Source=(LocalDb)\v11.0;Initial Catalog=aspnet-DbTestApp-20130205173443;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|\aspnet-DbTestApp-20130205173443.mdf" providerName="System.Data.SqlClient" />
I can't even create or manage the database, which I want to be generated from my models
public class Category
{
public int Id { get; set; }
[Required]
[MinLength(4)]
[MaxLength(64)]
public string Name { get; set; }
public virtual IEnumerable<Article> Articles { get; set; }
}
public class Article
{
public int Id { get; set; }
public int CategoryId { get; set; }
public virtual Category Category { get; set; }
[Required]
[MinLength(4)]
[MaxLength(64)]
public string Title { get; set; }
[Required]
[MinLength(16)]
[MaxLength(1024)]
[DataType(DataType.MultilineText)]
public string Content { get; set; }
[Required]
[Display(Name = "Post Anonymous?")]
public bool IsAnonymous { get; set; }
public int AuthorId { get; set; }
public virtual Author Author { get; set; }
public virtual IEnumerable<Comment> Comments { get; set; }
}
public class Author
{
public int Id { get; set; }
[MinLength(3)]
[MaxLength(64)]
public string AuthorName { get; set; }
public virtual IEnumerable<Article> Articles { get; set; }
public virtual IEnumerable<Category> Categories { get; set; }
}
public class Comment
{
public int Id { get; set; }
public int ArticleId { get; set; }
public virtual Article Article { get; set; }
[Required]
[MinLength(3)]
[MaxLength(64)]
public string Author { get; set; }
[MaxLength(64)]
public string Title { get; set; }
[Required]
[MinLength(4)]
[MaxLength(512)]
public string Content { get; set; }
}
using the context below
public class BlogContext : DbContext
{
public DbSet<Category> Categories { get; set; }
public DbSet<Article> Articles { get; set; }
public DbSet<Author> Authors { get; set; }
public DbSet<Comment> Comments { get; set; }
public BlogContext()
: base("DefaultConnection")
{
Configuration.ProxyCreationEnabled = false;
}
}
I also set the initializer in Global.asax Application_Start() method:
Database.SetInitializer(new DropCreateDatabaseIfModelChanges<BlogContext>());
The problem comes when I'm trying to call
var articles = db.Articles.Include(a => a.Category).Include(a => a.Author);
inside my BlogController's Index() method to return a view with the list of stored articles. That happens every time calling a DB related methods, the error message is:
Model compatibility cannot be checked because the database does not contain model metadata. Model compatibility can only be checked for databases created using Code First or Code First Migrations.
In the tutorials I found nothing about problems like that and the solutions I read couldn't solve the problem.
Any ideas?
Thanks

DropCreateDatabaseIfModelChanges requires there to be a previous model already in place before it will compare the two. To start up the database you'll need to use the DropCreateDatabaseAlways initializer.

Related

Entity framework relation mapping is not working

print("Hello World !");
I'm a beginner into the .NET world, i'm trying to use entity framework but i have a problem, my relations entities are not loaded, i followed the microsoft documentation but.
Here are my models :
[Table("Path")]
public class Path
{
[Key]
public int PathId { get; set; }
[Required]
public string Title { get; set; }
public List<Image> Images { get; set; }
[Required]
public string Description { get; set; }
[Required]
public float LocationX { get; set; }
[Required]
public float LocationY { get; set; }
[Required]
public string QrCode { get; set; }
public List<Mark> Marks { get; set; }
}
[Table("Mark")]
public class Mark
{
[Key]
public int MarkId { get; set; }
public int Value { get; set; }
public int PathForeignKey { get; set; }
[ForeignKey("PathForeignKey")]
public Path Path { get; set; }
}
[Table("Image")]
public class Image
{
[Key]
public int ImageId { get; set; }
[Required]
public string Name { get; set; }
public string Description { get; set; }
public int PathForeignKey { get; set; }
[ForeignKey("PathForeignKey")]
public Path Path { get; set; }
[Required]
public string Source { get; set; }
}
For me, everything is ok, i even have the correct entries into my database :
database entries
But my relation dont load when i'm reading database into my controller.
[HttpGet]
public IEnumerable<Path> GetAll()
{
return _context.Paths.ToList();
}
results
I'm sorry, i guess it is probably a stupid beginner error but i dont see what i did wrong. I tried with various database system, with virtual or not ...
Mark your navigation properties as virtual. For example:
[Table("Mark")]
public class Mark
{
[Key]
public int MarkId { get; set; }
public int Value { get; set; }
public int PathForeignKey { get; set; }
[ForeignKey("PathForeignKey")]
public virtual Path Path { get; set; } //Added virtual keyword
}
and a second way to load navigation properties is to use Include while fetching data:
return _context.Marks.Include(x => x.Path).ToList();

Map many to many objects using Entity Framework

For example we have profile and organisation. Both have articles.
public class Article
{
public int Id { get; set; }
public string Title { get; set; }
}
public class Profile
{
public int Id { get; set; }
public string Email { get; set; }
public virtual ICollection<Article> Articles { get; set; }
}
public class Organisation
{
public int Id { get; set; }
public string Name { get; set; }
public virtual ICollection<Article> Articles { get; set; }
}
In this way Article should have two kinds of parent so it should have something like parent type to be able to access a parent when you select articles directly.
public class Article
{
public int Id { get; set; }
public string Title { get; set; }
public int ParentId { get; set; }
public ArticleParentType Parent { get; set; }
}
Is it possible to map it using Entity Framework?
Is it a good idea to do it?
What is the best practice for storing this kind of data?
public class Article
{
public int Id { get; set; }
public string Title { get; set; }
public int ParentId { get; set; }
public ArticleParentType Parent { get; set; }
}
Is it possible to map it using Entity Framework?
Is it a good idea to do it?
Possible yes but not a good idea. The underlying Database can't use a foreign key for Parentid. It would be slow.
What is the best practice for storing this kind of data?
A simple approach, with 2 Nullable parents and without CascadeOnDelete:
public class Article
{
public int Id { get; set; }
public string Title { get; set; }
public virtual Profile Profile { get; set; }
public virtual Organisation Organisation { get; set; }
}
Alternatively you could use inheritance for Article, ie class OrganisationArticle : Article {}

Why do all of my model classes get added to the first Configuration.cs file that gets created when I'm using multiple Data Context classes?

Entity Framework 6 Code First Migrations with Multiple Data Contexts
NOTE: This is my first post on Stack Overflow so please let me know if I need to provide more detail.
FYI: I have read the article:
Entity Framework 6 Code First Migrations with Multiple Data Contexts
And I'm trying to do something similar. My question/problem revolves around the CreateTable methods generated in the Configuration.cs file.
I have 29 model classes, and I'm trying to set up 4 different Data Contexts.
In my first Data Context, RulingRequestContext, I have 11 of my 29 model classes:
public class RulingRequestContext : BaseContext<RulingRequestContext>
{
public DbSet<RulingRequest> RulingRequests { get; set; }
public DbSet<Agency> Agencies { get; set; }
public DbSet<RulingRequestGroup> RulingRequestGroups { get; set; }
public DbSet<RulingRequestOverallOutcome> RulingRequestOverallOutcomes { get; set; }
public DbSet<RulingRequestType> RulingRequestTypes { get; set; }
public DbSet<RulingRequestResult> RulingRequestResults { get; set; }
public DbSet<Issue> Issues { get; set; }
public DbSet<Decision> Decisions { get; set; }
public DbSet<RulingRequestRoutingInformation> RulingRequestRoutingInformations { get; set; }
public DbSet<Staff> Staffs { get; set; }
public DbSet<StatusOfRulingRequest> StatusOfRulingRequests { get; set; }
Using Package Manager, here are 3 steps I'm following:
NOTE: These are the generic instructions:
(1) enable-migrations -ContextTypeName <DbContext-Name-with-Namespaces> -MigrationsDirectory:<Migrations-Directory-Name>
(2) Add-Migration -configuration <DbContext-Migrations-Configuration-Class-with-Namespaces> <Migrations-Name>
(3) Update-Database -configuration <DbContext-Migrations-Configuration-Class-with-Namespaces> -Verbose
Step 2 is where I have my question. After I run Step 2, I get: a folder, called RulingRequestContextMigrations with 2 files:
201502212023508_Initial.cs and Configuration.cs.
When I look at Configuration.cs. file, the function, public override void Up(), has a CreateTable function for all 29 of my model classes.
I thought that only the 11 listed in my RulingRequestContext class would be created.
Here is a snapshot of my RulingRequest model class.
public class RulingRequest
{
public RulingRequest()
{
this.RulingRequestResults = new HashSet<RulingRequestResult>();
this.RulingRequestRoutingInformations = new HashSet<RulingRequestRoutingInformation>();
}
[DisplayName("Ruling Request ID")]
public int RulingRequestID { get; set; }
[DisplayName("Employee ID Number")]
public string EmployeeIDNumber { get; set; }
[DisplayName("Case Group")]
public string RulingRequestGroupID { get; set; }
[DisplayName("Type")]
public Nullable<int> RulingRequestTypeID { get; set; }
[DisplayName("First Name")]
public string FirstName { get; set; }
[DisplayName("Last Name")]
public string LastName { get; set; }
public string MI { get; set; }
public string Suffix { get; set; }
public string Address { get; set; }
[DisplayName("Address 2")]
public string Address2 { get; set; }
public string City { get; set; }
public string State { get; set; }
public Nullable<int> Zip { get; set; }
[DisplayName("Home Phone")]
public string HomePhone { get; set; }
[DisplayName("Work Phone")]
public string WorkPhone { get; set; }
public string Email { get; set; }
[DisplayName("Agency")]
public string AgencyNumber { get; set; }
[DisplayName("Date Received")]
public Nullable<System.DateTime> DateReceived { get; set; }
[DisplayName("Grievance Initiation Date")]
public Nullable<System.DateTime> GrievanceInitiationDate { get; set; }
[DisplayName("Decision Date")]
public Nullable<System.DateTime> DecisionDate { get; set; }
[DisplayName("Re-Activation Date")]
public Nullable<System.DateTime> ReActivationDate { get; set; }
[DisplayName("Overall Outcome")]
public Nullable<int> RulingRequestOverallOutcomeID { get; set; }
[ScaffoldColumn(false)]
public string CreatedBy { get; set; }
[ScaffoldColumn(false)]
public Nullable<System.DateTime> CreatedDatetime { get; set; }
[ScaffoldColumn(false)]
public string UpdatedBy { get; set; }
[ScaffoldColumn(false)]
public Nullable<System.DateTime> UpdatedDatetime { get; set; }
public virtual Agency Agency { get; set; }
public virtual RulingRequestGroup RulingRequestGroup { get; set; }
public virtual RulingRequestOverallOutcome RulingRequestOverallOutcome { get; set; }
public virtual RulingRequestType RulingRequestType { get; set; }
public virtual ICollection<RulingRequestResult> RulingRequestResults { get; set; }
public virtual ICollection<RulingRequestRoutingInformation> RulingRequestRoutingInformations { get; set; }
}
All 4 of my Data Context classes have a model class that uses Agency and has:
public virtual Agency Agency { get; set; }
QUESTIONS:
(1) Are all 29 of my model classes being added to the RulingRequest's Configuration.cs because of the relationships/virtual properties? For example since all 4 of my Data Context classes have: public virtual Agency Agency { get; set; } all of those model classes are being added to the Configuration.cs file for my RulingRequestContext.cs class.
(2) Assuming my initial Data Context class (RulingRequestContext) and it's Configuration.cs class are working as they are supposed to, should I comment out all of the CreateTable code in the 3 other Configuration.cs classes?
(3) Or should I comment out the CreateTable code for all of the model classes that don't appear as DBSet properties in my RulingRequestContext? And then do the same for my 3 other DataContext classes?
Thanks for your time reading this! Please let me know if there's anything I can do or not do to make my posts better in the future!
Yes, it is grabbing the related entities. Your link talks about commenting out the tables from the other contexts "Before running update command, commented out the generated code for Users tables as shown above. Since Users table is already created by first DbContext migrations."

EF6 generates all the entity classes (tt .cs) in single file (eg. model.cs) and not as separate .cs file

I am using EF6. I have 2 tables Events and Users for which I used database first approach to generate the EDMX models and entity classes. Everything works fine but the entity classes are coming under a single file in this case EventSample.cs(shown below). I am not getting separate files for each entity as Event.cs and User.cs. Please let me know if this is correct and if not how to rectify it?
public partial class Event
{
public Event()
{
this.Users = new HashSet<User>();
}
public int Id { get; set; }
public string Title { get; set; }
public System.DateTime Time { get; set; }
public string Location { get; set; }
public string Description { get; set; }
public int Creator_Id { get; set; }
public virtual User User { get; set; }
public virtual ICollection<User> Users { get; set; }
}
public partial class User
{
public User()
{
this.Events = new HashSet<Event>();
this.Events1 = new HashSet<Event>();
}
public int Id { get; set; }
public string Username { get; set; }
public string Password { get; set; }
public virtual ICollection<Event> Events { get; set; }
public virtual ICollection<Event> Events1 { get; set; }
}

Linking MVC4 Models By ID

Woke up yesterday wondering what all the fuss with MVC was about. So I found out and wow it's nice and stuff like that.
I'm used to using one related Linq to Sql DataContext DB in ASP.NET projects.
//Some ASP.NET Page Codebehind
DataContext dbEntire = new DataContext()
With MVC4 with the Entity framework you make the model and it creates a DB for you.
//Models/Article/Article.cs
public class ArticleDBContext: DbContext
{
public DbSet<Article> Articles { get; set; }
}
public class Article
{
public int ID {get;set;}
public string Title{ get; set; }
public int AuthorID { get; set; }
public string Body { get; set; }
public int CategoryID { get; set; }
public DateTime Submitted{ get; set; }
public DateTime LastModified { get; set; }
}
Author and Category are seperate Models.
But how do you set up all the database relations.. One to many, etc. etc. ?
public class ArticleDBContext: DbContext
{
public DbSet<Article> Articles { get; set; }
public DbSet<Category> Categories { get; set; }
}
public class Article
{
public int ID {get;set;}
public string Title{ get; set; }
public int AuthorID { get; set; }
public string Body { get; set; }
public int CategoryID { get; set; }
public virtual Category Category { get; set; }
public DateTime Submitted{ get; set; }
public DateTime LastModified { get; set; }
}
public class Category
{
public int ID {get;set;}
public string Name{ get; set; }
public virtual ICollection<Article> Articles { get; set; }
}
Go through some tutorials on Entity Framework Code First.
EF Code First and MVC
EF getting started