DeleteDatabase is not supported by the provider - ef-code-first

I'm using EF Code First with SQL Express 2012.
Everything worked fine until now. I'm getting this error:
DeleteDatabase is not supported by the provider.
public class SqlServerContext : DbContext
{
public DbSet<Estimate> Estimates { get; set; }
public SqlServerContext(String connectionString)
:base(connectionString)
{
Database.SetInitializer(new SqlServerContextInitializer());
}
}
public class SqlServerContextInitializer : DropCreateDatabaseAlways<SqlServerContext>
{
}
Did anyone have similar problem?

Related

System.Data.SqlClient not found in Repository Constructor

I'm trying to build a data layer in my application based on a .NET Core class library using Dapper. The data classes look like this:
//FieldRepository.cs
using Dapper.Contrib.Extensions;
public class FieldRepository : IRepository<TblField>
{
private IDbConnection connection;
public FieldRepository(string connectionString)
{
connection = new SqlConnection(connectionString);
}
public IEnumerable<TblField> GetAll()
{
return connection.GetAll<TblField>();
}
}
//IRepository.cs
public interface IRepository<T>
{
IEnumerable<T> GetAll();
}
//TblField
public class TblField
{
public string FieldText { get; set; }
public int Id { get; set; }
}
Then I tried to run a test against these classes as below:
public void ThereShouldBeFields()
{
var repo = new FieldRepository("valid connection string");
var fields = repo.GetAll();
fields.Should().NotBeNull();
}
When I ran this test, I got a FileNotFound exception at the FieldRepository constructor for System.Data.SqlClient, version 4.2.0.0, which is installed in the data layer project.
I know I'm missing something simple here, but what is it?
Looks like it was because the reference needed to be added to both the Test project and the Data layer project.
The more you know!

Implementing ASP.NET Core Identity in a n-tier application using EF7 and SQLite

I'm creating an n-tier application in .NET core in which I try to keep the Identity framework out of my presentation layer as much as possible. Since I'm going to use a linux server to run my application, I'll be using Sqlite databases to persist my domain and all identity related classes.
All documentation I've encountered (official ms documentation and various blogs, tutorials ...) doesn't use SQLite.
In my datalayer I have a DbContext class which inherits from the ASP.NET's core IdentityDbContext.
public class SquiriusDbContext : IdentityDbContext<User>
{
public DbSet<Blog> Blogs { get; set; }
public DbSet<Post> Posts { get; set; }
public DbSet<Article> Articles { get; set; }
public DbSet<HtmlSection> HtmlSections { get; set; }
public DbSet<MediaSection> MediaSections { get; set; }
public DbSet<Comment> Comments { get; set; }
public DbSet<Category> Categories { get; set; }
public DbSet<Tag> Tags { get; set; }
public SquiriusDbContext() { }
public SquiriusDbContext(DbContextOptions options) : base(options)
{
Database.Migrate();
}
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
var connectionStringBuilder = new SqliteConnectionStringBuilder { DataSource = "squiriusdb.db" };
var connectionString = connectionStringBuilder.ToString();
var connection = new SqliteConnection(connectionString);
optionsBuilder.UseSqlite(connection);
}
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
}
}
When I try to perform a migration, I'm getting the following error:
PM> add-migration identity
An error occurred while calling method 'ConfigureServices' on startup class 'Startup'. Consider using IDbContextFactory to override the initialization of the DbContext at design-time. Error: GenericArguments[0], 'Squirius.Domain.Authentication.User', on 'Microsoft.AspNetCore.Identity.EntityFrameworkCore.UserStore`4[TUser,TRole,TContext,TKey]' violates the constraint of type 'TUser'.
System.InvalidOperationException: The entity type 'User' requires a primary key to be defined.
The error says that the entity type 'User' requires a primary key to be defined, which is strange since this User class inherits directly from Microsoft.AspNetCore.Identity.EntityFrameworkCore.IdentityUser.
I think this might have something to do with the limitations of the EF7 Sqlite provider, and using an ms sql server database would solve this error.
Does anyone know a way around this so I can use an Sqlite db in order to be able to build for a linux platform?
Thanks!

scaffolding Error There is no implicit reference conversion from Configuration to System.Data.Entity.Migrations.DbMigrationsConfiguration

I utuliz EF 6.0.2 Code First, VS 2013, MVC 5 and have dbContext and configuration class derived from dbMigrationConfiguratino class like this:
public class Db : DbContext
{
public Db()
{
Database.SetInitializer(new MigrateDatabaseToLatestVersion<Db,Migrations.Configuration -- my Derived dbMigrationConfig Classs In Migrations folder-->());
}
public DbSet<ItemCat> ItemCats { get; set; }
public DbSet<Item> Items { get; set; }
}
and
public sealed class Configuration : DbMigrationsConfiguration<Db>
{
public Configuration()
{
AutomaticMigrationsEnabled = true;
AutomaticMigrationDataLossAllowed = true;
}
protected override void Seed(Db context)
{
}
}
From here Implicit refrience conversion for From any
class-type S to any class-type T, provided S is derived from T.
is valid. like my configuration and DbMigrationConfiguration
but when want add View for a Controller get error:
Microsoft Visual Studio
Error
There was an error running the selected code generator:
'There was an error in compilation of the type 'DAL.Db'.
CS0311: The type 'DAL.Migrations.Configuration' cannot be used as
type parameter 'TMigrationsConfiguration' in the generic type or
method
'System.Data.Entity.MigrateDatabaseToLatestVersion'.
There is no implicit reference conversion from
'DAL.Migrations.Configuration' to
'System.Data.Entity.Migrations.DbMigrationsConfiguration'.'
OK
Edit:
find this:
CS3011 Error
Edit: I figure out my Db Class To :
public class Db :MigrateDatabaseToLatestVersion<DbContext,Migrations.Configuration>
{
//Database.SetInitializer(new MigrateDatabaseToLatestVersion<Db,Migrations.Configuration>());
}
and get same bolded above error compile time

ASP.NET MVC4: IQueryable does not contain a definition for 'Add'

I have tried to make an MVC news system. I started by the use a pluralsight tutorial which was used to create a department with employees. I changed the idea to fit my own purposes, changing the departments to "category" and employees to "newspost". This all works out fine, but now I want to remove the categories, since I don't need categories for my news system. But I can't add to the database using entityframework when I do this.
I have an interface for the datasource that looks like this:
INewMvcSiteDataSource
public interface INewMvcSiteDataSource
{
IQueryable<NewsPost> NewsPosts { get; }
void Save();
}
This is inherited by my DB Context class:
NewMvcSiteDb
public class NewsMvcSiteDb : DbContext, INewMvcSiteDataSource
{
public NewsMvcSiteDb() : base("DefaultConnection")
{
}
public DbSet<NewsPost> NewsPosts { get; set; }
IQueryable<NewsPost> INewMvcSiteDataSource.NewsPosts
{
get { return NewsPosts; }
}
public void Save()
{
SaveChanges();
}
}
I then want to use it in the controller to add a newspost to the database:
NewsController
var newsPost = new NewsPost()
{
Subject = newsModel.Subject,
Content = newsModel.Content,
ImagePath = newsModel.ImagePath
};
_db.NewsPosts.Add(newsPost);
_db.Save();
But this is where the ADD fails with the message: 'System.Linq.IQueryable' does not contain a definition for 'Add' and no extension method 'Add' accepting a first argument of type 'System.Linq.IQueryable' could be found (are you missing a using directive or an assembly reference?)
Now as the error says, its caused by using IQueryable, but I have no idea how else to do it.
Can you guys help?
Thanks.
If you don't mind exposing DbSet via your interface (some people don't like the ORM bleeding into the application),you should be able to do the following:
public interface INewMvcSiteDataSource
{
DbSet<NewsPost> NewsPosts { get; }
void Save();
}
public class NewsMvcSiteDb : DbContext, INewMvcSiteDataSource
{
public NewsMvcSiteDb() : base("DefaultConnection")
{
}
public DbSet<NewsPost> NewsPosts { get; set; }
public void Save()
{
SaveChanges();
}
}

how can i solve : the type or namespace name 'DbModelBuilder' Could not be found

i created an MVC Project for test on another PC last night
it worked good on that PC but when i want to build it on my own PC it i give error :
the type or namespace name 'DbModelBuilder' Could not be found(are you missing...)
how can i solve it ?
using System.Data.Entity;
using System.Data.Entity.ModelConfiguration.Conventions.Edm.Db;
namespace University.Models
{
public class SchoolContext : DbContext
{
public DbSet<Student> Students { get; set; }
public DbSet<Enrollment> Enrollments { get; set; }
public DbSet<Course> Courses { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
}
}
}
Does this dll exist in your solution?
If it points to DbModelBuilder.dll in a folder in the other PC, it won't run successfully in your PC.
Edit:
Can you see this ?
Looks like you forgot an assembly reference to System.Data.Entity.

Resources