Entity Framework -Update-Database- Does not create Database - asp.net-mvc

Visual Studio 2013
I am trying to learn asp.net MVC over at PluralSight. I created a project(dll) called eManagr.Domain with the following classes:
Department / Employee / IDepartmentDatasource
Department.cs
public class Department
{
public virtual int Id { get; set; }
public virtual string Name { get; set; }
public virtual ICollection<Employee> Employees { get; set; }
}
Employee.cs
public class Employee
{
public virtual int Id { get; set; }
public virtual string Name { get; set; }
}
IDepartmentDataSource
public interface IDepartmentDataSource
{
IQueryable<Employee> Employees { get; }
IQueryable<Department> Departments { get; }
}
I created an infrastructure folder with the following file : DepartmentDb.cs
public class DepartmentDb : DbContext, IDepartmentDataSource
{
public DbSet<Employee> Employees {get; set;}
public DbSet<Department> Departments {get; set;}
IQueryable<Employee> IDepartmentDataSource.Employees
{
get { return Employees; }
}
IQueryable<Department> IDepartmentDataSource.Departments
{
get { return Departments; }
}
}
I then created another project using MVC 4 called eManager.Web with Internet Template during the creation of the project.
When running Enable-Migration it says I have two[eWeb.Domain , eWeb.Model.Users] which then I tell it Enable-Migration with the following command:
Enable-Migration -ContextTypeName DepartmentDb
which creates the migration folder and a file called Configurations.cs
public Configuration()
{
AutomaticMigrationsEnabled = true;
}
protected override void Seed(eManager.Web.Infrastructure.DepartmentDb context)
{
context.Departments.AddOrUpdate(t => t.Name,
new Department() { Name="Engineering"},
new Department() { Name = "Sales" },
new Department() { Name = "Shipping" },
new Department() { Name = "HR" }
);
}
EDIT -- Connection String from Web.Config --
<add name="DefaultConnection" connectionString="Data Source=(LocalDb)\v11.0;Initial Catalog=aspnet-eManager.Web-20140216202751;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|\aspnet-eManager.Web-20140216202751.mdf" providerName="System.Data.SqlClient" />
When I run the following I get the following reponse:
PM> update-database
Specify the '-Verbose' flag to view the SQL statements being applied to the target database.
No pending code-based migrations.
Running Seed method.
PM>
After this runs, I suppose to see a database file in my App_Data but it does not exist and when I use SQL Server Object Explorer, the database is not created even though that is what I am trying to do.

Could you provide your connection string from Web.config?
Also, is there a Data Connection (Server Explorer -> Data Connections) named the same as your connection String?
I think, adding a parameter-less constructor to your DepartmentDb context class could solve your problem
public DepartmentDb ()
: base("name=DefaultConnection")
Where name=DefaultConnection has to be your connection string name

I noticed that you enabled your migration in the correct way, have you run:
add-migration "give it a name" ?
once this has been completed you will notice a new file in the migrations folder.
you wont be able to update database with out creating a new migration.

I just ran into something very similar. I encountered it when I was going through the following ASP.NET MVC tutorial:
https://www.asp.net/mvc/overview/older-versions/getting-started-with-ef-5-using-mvc-4/creating-an-entity-framework-data-model-for-an-asp-net-mvc-application
In my case, the problem seemed to be that I already had a database table of that name. I had gone through the tutorial partway previously about a month ago, but was interrupted and had to abort. Even though I deleted the entire Project, it seemed that the database table name may have been retained.
I say seemed to be, because the problem disappeared with the following solution: after a 2nd delete of the project, I carefully substituted 'Contoso' for 'ContosoUniversity' in every relevant situation.
Before the problem was solved, I was repeatedly getting the (0x80131904-error) in the Package Manager Console when trying to update-database, and a notice that the mdf file could not be connected to the database. However, when I checked the appropriate directory, the mdf file was not even being created.
FYI For beginning MVC-ers in Visual Studio 2012, I do recommend going through the following MVC tutorial before the one above.
https://www.asp.net/mvc/overview/older-versions/getting-started-with-aspnet-mvc4/intro-to-aspnet-mvc-4
A MVC5 Visual Studio 2013 version is also available through that link.
The tutorial of the first paragraph makes a few jumps...
I could not have debugged the issue if I'd started with the EF5 tutorial as my first MVC project.

Related

How do i create a class library proejct to handle my migrations and database objects in entity framework core 2.2?

I have created a .net core 2.2 web mvc application. I then created a .net core 2.2 class library to contain all my database models. When attempting to run the initial migration i get the error cannot create object of type 'X'.
I have referenced my class library in the main web project, set it as the startup and in the package manager console i set the default project as the class library.
I expected this to create my migrations but i get this error.
public class ProjectWalesDbContext : DbContext
{
public ProjectWalesDbContext(DbContextOptions<ProjectWalesDbContext> options)
: base(options)
{ }
public DbSet<User> Users { get; set; }
public DbSet<UserCourse> UserCourses { get; set; }
public DbSet<Course> UserCourse { get; set; }
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<User>()
.Property(b => b.created_at)
.HasDefaultValueSql("getdate()");
modelBuilder.Entity<User>()
.Property(b => b.password_reset_date)
.HasDefaultValueSql("getdate()");
modelBuilder.Entity<User>()
.Property(b => b.password_reset_required)
.HasDefaultValueSql("0");
modelBuilder.Entity<UserCourse>()
.Property(b => b.course_progress)
.HasDefaultValueSql("0");
}
}
this is my db context that i set up ive set it up the same way in other projects so i cant see what ive done wrong if anything. any help would be greatly appreciated
Solved, I needed to add the line:
services.AddDbContext<ProjectWalesDbContext>(item => item.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));
to the start-up of the web app so I could create the context.

add-migration 'InitialModel' is showing database access error

After enabling the migrations, I tried to add the first migration using add-migration 'InitialModel'. I am attaching the The MyDBContext.cs file code.
{
public class MyDBContext : DbContext
{
public DbSet<Customer> Customers { get; set; } // My domain models
public DbSet<Movie> Movies { get; set; } // My domain models
}
}
An error occurred accessing the database. This usually means that the connection to the database failed. Check that the connection string is correct and that the appropriate DbContext constructor is being used to specify it or find it in the application's config file. See http://go.microsoft.com/fwlink/?LinkId=386386 for information on DbContext and connections. See the inner exception for details of the failure.

Sitecore PageEditor randomly shows error with Glass.Mapper

I sometimes receive an error when I open a page in page editor mode on my Sitecore site using Glass.Mapper.
You cannot save a class that does not contain a property that
represents the item ID. Ensure that at least one property has been
marked to contain the Sitecore ID. Type:
Castle.Proxies.IBasicPageProxy
at Glass.Mapper.Sc.Configuration.SitecoreTypeConfiguration.ResolveItem(Object
target, Database database)
at Glass.Mapper.Sc.GlassHtml.MakeEditable[T](Expression'1 field, Expression'1 standardOutput, T model, Object parameters,
Context context, Database database, TextWriter writer)
This appears in the place of the rendering, so it doesn't show up as a standard ASP error.
Restarting IIS solves this, but soon it appears again..
I use interfaces for model definitions, with every single interface inheriting from IBaseType:
[SitecoreType]
public interface IBaseType
{
[SitecoreId]
Guid Id { get; set; }
[SitecoreInfo(SitecoreInfoType.Name)]
string Name { get; set; }
[SitecoreItem]
Item InnerItem { get; set; }
[SitecoreInfo(SitecoreInfoType.Url)]
string Url { get; set; }
[SitecoreInfo(SitecoreInfoType.TemplateId)]
Guid TemplateId { get; set; }
[SitecoreInfo(SitecoreInfoType.FullPath)]
string FullPath { get; set; }
}
I use Sitecore 7.5 rev. 141003 (.NET 4.5, MVC5), and the latest versions of Glass.Mapper related NuGet packages currently:
Castle.Windsor 3.3.0
Castle.Core 3.3.3
Glass.Mapper 3.0.14.26
Glass.Mapper.Sc 3.2.3.46
Glass.Mapper.Sc.CastleWindsor 3.3.0.24
Glass.Mapper.Sc.Mvc-5 3.3.0.43
The issue appears on all machines we tried, but all of them uses Windows 8, IIS 8.5.9600. I tried reverting to the WebActivator method, but it didn't help. Model definitions are in a separate class library project, which references all glass mapper assemblies.
I'm pretty clueless, never run into this error before on other projects. Does anyone have any idea what can cause this, or how I could debug it?
Thank you for your help!
I have put below code in my model and it works
[SitecoreId]
public virtual Guid Id { get; set; }
Glass mapper requires Item to be represented by a ID. If the glass model does not have a property for GUID, you will see this error.
You can fix this by adding the property public virtual GUID Id {get; set;} to your glass model

Publishing with Multiple Context in Same Database

I encountered a problem with multiple context in EF 6. Recently i had splitted my context into three parts and configured them as had been told here
Everything was fine, until i decided to publish via Visual Studio; because publish wizard detected only one of my context instead of three. And interestingly everytime it detects same context, i couldn't find why, neither first letter of name nor any difference from the others seem cause this.
But i couldn't publish my MVC project because of this. I have to migrate all three contexts while publishing.
After some search, i saw Update-Database command gets connectionstring parameter. This is my last option, if there isn't any way to solve publish wizard i try to update database with this code.
I haven't been able to reproduce this issue. Here are the steps I used (using Visual Studio 2013 Update 2).
Create a new MVC application. Add the following models to the project (two separate Code First models/contexts).
public class CustomerContext : DbContext
{
public DbSet<Customer> Customers { get; set; }
}
public class Customer
{
public int CustomerId { get; set; }
public string Name { get; set; }
}
public class ProductContext : DbContext
{
public DbSet<Product> Products { get; set; }
}
public class Product
{
public int ProductId { get; set; }
public string Name { get; set; }
public decimal Price { get; set; }
}
Then enable migrations, add a migration and update the local database for both contexts, using the following commands.
Enable-Migrations -ContextTypeName CustomerContext -MigrationsDirectory Migrations\Customer
Enable-Migrations -ContextTypeName ProductContext -MigrationsDirectory Migrations\Product
Add-Migration FirstMigration -ConfigurationTypeName MyWebApp.Migrations.Customer.Configuration
Add-Migration FirstMigration -ConfigurationTypeName MyWebApp.Migrations.Product.Configuration
Update-Database -ConfigurationTypeName MyWebApp.Migrations.Customer.Configuration
Update-Database -ConfigurationTypeName MyWebApp.Migrations.Product.Configuration
Then when I right-click -> Publish the project I get the option to enable migrations on App_Start for both of my contexts (and the ASP.NET Identity context too). If I understand correctly, you are not seeing your additional context(s) in this screen.
I've seen this happen when multiple DbContexts share a common connection string. By this I mean:
public class Context1: DbContext
{
public Context1()
: this("DefaultConnection")
{}
public Context1: (string connectionString)
: base(connectionString)
{}
....
}
public class Context2: DbContext
{
public Context2()
: this("DefaultConnection")
{}
public Context2: (string connectionString)
: base(connectionString)
{}
...
}
When you Publish, only one DbContext will show up under Settings > Databases. If you change "DefaultConnection" to something else then you will see the distinct DbContexts. Like this:
public class Context1: DbContext
{
public Context1()
: this("DefaultConnection")
{}
public Context1: (string connectionString)
: base(connectionString)
{}
....
}
public class Context2: DbContext
{
public Context2()
: this("DefaultConnection2")
{}
public Context2: (string connectionString)
: base(connectionString)
{}
...
}
Maybe this explains the behavior you are seeing.
If both dbContexts are using the same database (and therefore the same database connection string in web.config), how do we get Web Deploy to show them both? Do I have to create a separate (duplicate) connection string that points to the same database just to get it to show up as a separate context in the wizard?
When you want to see all contexts in the Publish dialog, you need to add another connection strings to web.config. They should have different name and be referenced from your context (name in constructor)

Unable to retrieve metadata for 'MyService.Entities.Email'. The type initializer for 'System.Data.Entity.Internal.AppConfig' threw an exception

I am trying out the new Visual Studio 11 Beta with MVC 4.0 and EF 4.3.1.
When adding a controller I get the following message "Unable to retrieve metadata for 'MyService.Entities.Email'. The type initializer for 'System.Data.Entity.Internal.AppConfig' threw an exception"
Repro
I have a basic a solution which has an Entities project (MyService.Entities) and and MVC project (MyService.Website).
In the Entities project I have the bare bones for code first with a class for "Email" and a class for the DbContext. The two classes look like:
EntitiesDb
namespace MyService.Entities
{
public class EntitiesDb : DbContext
{
public EntitiesDb(string nameOrConnectionString)
: base(nameOrConnectionString)
{ }
public EntitiesDb()
{
}
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
// Sets up the defaults for the model builder
// Removes the metadata being written to the database, This is being depreciated anyhow
modelBuilder.Conventions.Remove<IncludeMetadataConvention>();
// Sets the table names so that they are named as a none plural name
modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
// Creates the database from scratch when it builds
base.OnModelCreating(modelBuilder);
}
public DbSet<Email> Emails { get; set; }
}
}
Email
namespace MyService.Entities
{
public class Email
{
public Email()
{
}
[Key, DatabaseGenerated(System.ComponentModel.DataAnnotations.DatabaseGeneratedOption.Identity)]
public int EmailId { get; set; }
public string Subject { get; set; }
public string BodyHTML { get; set; }
}
}
I have referenced the MyService.Entities project in the MyService.Website MVC project and set the Connection String to fit my Database name
<add name="EntitiesDb" connectionString="Data Source=localhost;Integrated Security=SSPI;Initial Catalog=EntitiesDb;MultipleActiveResultSets=True" providerName="System.Data.SqlClient" />
When I go to create the MVC scaffolding by doing the following:
Right click on Controllers folder > Add > Controller
Set controller name to be EmailController
Set Template to Controller with read/write actions and views, using Entity Framework
Set Model class to Email(MyService.Entities)
Set Data context class to EntitiesDb (MyService.Entities)
Set Views as Razor
It shows a message of
"Unable to retrieve metadata for 'MyService.Entities.Email'. The type initializer for 'System.Data.Entity.Internal.AppConfig' threw an exception"
The confusing thing is that it worked initially and then I started to add some more entities and Properties into the database and then it wouldn't allow me. I've now scaled right back and it is still doing it.
I have tried a few things after looking on the forums:
Checking the Connection string is correct in MVC Project Adding a
Connection string in the Entities Project
Reinstalled Visual Studio 11 Beta in case any updates have affected it
Checking that the references match (they are both EntityFramework
Version 4.3.1.0)
Dropping the database to rebuild (Now I don't have one ;o))
Is this a bug in VS11? Am I missing a reference to the metadata somewhere?
Any help much appreciated.
EDIT:
By un-installing the EntitiesFramework and re-installing the NuGet package fixed the problem.
Remove this section from webconfig
<entityFramework>
<defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework" />
<providers>
</providers>
</entityFramework>

Resources