I've added a simply Product-class (shown below) but when I run add-migration it generates an empty script. I guess this is hard to troubleshoot but any idea as to why this is?
public class Product
{
public int ProductID { get; set; }
[Required]
public string Name { get; set; }
[DataType(DataType.Currency)]
public decimal Price { get; set; }
}
public class ProductDBContext : DbContext
{
public DbSet<Product> Products { get; set; }
}
The migration file looks like this:
public partial class test : DbMigration
{
public override void Up()
{
}
public override void Down()
{
}
}
Have you added the according Class to your DB context?
public System.Data.Entity.DbSet YourDbSetName { get; set; }
Try Clearing out _MigrationHistory (and possible also opening your Project.Data.csproj to manually delete te migrations pending)
I found the answer here: solution
Related
Kinda stuck here. Any help would be appreciated. I'm trying to move two database classes to another namespace :
public class UserNotificationSubscription
{
public int Id { get; set; }
[ForeignKey("Employee")] public int EmployeeId { get; set; }
public virtual Employee Employee { get; set; }
public int NotificationSubscriptionId { get; set; }
[ForeignKey("NotificationSubscriptionId")]
public virtual CustomerNotificationSubscription CustomerNotificationSubscription { get; set; }
public bool IsInternalNotificationEnabled { get; set; }
public bool IsExternalNotificationEnabled { get; set; }
[ForeignKey("Customer")] public int CustomerId { get; set; }
public Customer Customer { get; set; }
}
public class CustomerNotificationSubscription
{
public int Id { get; set; }
public NotificationEvent NotificationEvent { get; set; }
public NotificationCategory NotificationCategory { get; set; }
public DateTime? ChangedDate { get; set; }
[ForeignKey("ChangedByEmployee")] public int? ChangedByEmployeeId { get; set; }
public Employee ChangedByEmployee { get; set; }
public bool Enabled { get; set; }
[ForeignKey("Customer")] public int? CustomerId { get; set; }
public Customer Customer { get; set; }
public string EmployeesJson { get; set; }
public IList<UserNotificationSubscription> UserNotificationSubscriptions { get; set; }
}
but when I move them, update references, build and try I get the "The model backing the 'IdentityDb' context has changed since the database was created." error. So I try to add a migration to see if anything's changed, and this migration is genereated:
public partial class Test : DbMigration
{
public override void Up()
{
DropColumn("dbo.AspNetUsers", "Password");
DropColumn("dbo.AspNetUsers", "Discriminator");
}
public override void Down()
{
AddColumn("dbo.AspNetUsers", "Discriminator", c => c.String(nullable: false, maxLength: 128));
AddColumn("dbo.AspNetUsers", "Password", c => c.String());
}
}
I have no idea why this is happening. Never seen that happened before. I've already moved a bunch of other classes with no problem, but these are the 2 I had left and then this happens. The new namespace is in another project called Domain. I even tried "faking" the namespace in the original project aka using the new namespace in the old cs file, and that also works fine. And then I tried commenting out the "old" cs file, and uncommenting the "new", and then it crashes again. I've tried using Resharper's built in move tool and that also makes it crash.
I use VS 2015 and Entity Framework 6.2.0.
I have two tables tblUsers and tblFields in sqlServer.
I create two models related to my tables:
[Table("tblUsers")]
public class Users
{
[Key]
public string UserName { get; set; }
public string UserPassword { get; set; }
public DateTime RegisterDateTime { get; set; }
public string FieldId { get; set; }
public float Score { get; set; }
}
[Table("tblFields")]
public class Fields
{
[Key]
public string FieldID { get; set; }
public string FieldTitle { get; set; }
}
So, in my DataAccessLayer, I have:
public class Dal : DbContext
{
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<Users>().ToTable("tblUsers");
modelBuilder.Entity<Fields>().ToTable("tblFields");
base.OnModelCreating(modelBuilder);
}
public DbSet<Users> UsersSet { get; set; }
public DbSet<Fields> FieldsSet { get; set; }
}
When I use the following code in BusinessLayer to get Users Data:
public List<Users> GetAllUsers()
{
Dal dal = new Dal();
return dal.UsersSet.ToList();
}
No users are return!
Where is the problem?
I create a database and it's tables. Then I created my project that used from database. The problem is here!
Solution: I deleted all tables from database and then I run my application. Application created all tables again and problem is solved.
I have view in SQL called ViewTest.
In code I have this model
[Table("dbo.ViewTest")]
public class ViewTest
{
public int Id { get; set; }
public int EmployeeID { get; set; }
public string PreferredName { get; set; }
public string EmailPrimaryWork { get; set; }
public string GeoCoverage { get; set; }
public string Role { get; set; }
public bool? LeftEmpFlag { get; set; }
}
In configuration file:
public virtual IDbSet<ViewTest> ViewTests { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Configurations.Add(new ViewConfiguration());
}
public class ViewConfiguration : EntityTypeConfiguration<ViewTest>
{
public ViewConfiguration()
{
this.HasKey(t => t.Id);
this.ToTable("ViewTest");
}
}
I want to have connection like this one ViewTests.Where(....) ,but when I tried to do like this way I have error There is already an object named 'ViewTest' in the database..This means entity framework try to create new Table and I don`t want this.I want to access this view only!
Well, it is code first so you should probably create your view in code. If that is not possible, then you need to tell EF not to create the table by commenting that line out of the Up() method on the migration. Once you update-database EF will have it in the metadata and you should be good to go.
I am using Visual Studio Express 2013 for Web (specifically version 12.0.21005.1 REL). This is my first project using VS2013, I've been using VS2012 up until this point.
I am attempting to create a new controller in my asp.net MVC application. I am using Entity Framework 5 with code first (.NET 4.5). I want Visual Studio to create the template for me (you know, a controller with views to read/write/delete etc, rather than write the code myself from scratch).
However, every time I try to create the controller I get the following error message:
Is there some sort of bug in VS 2013? I can't figure out what this means, and restarting VS2013 does not help.
Here are the gory details.... actually it is all very simple since this is a new project with very little code written so far.
My model:
namespace ProfessionalSite.Models
{
public class EntityModels
{
public class Student
{
public int ID { get; set; }
public string LastName { get; set; }
public string FirstMidName { get; set; }
public virtual ICollection<Enrollment> Enrollments { get; set; }
}
public class Enrollment
{
public int ID { get; set; }
public string EnrollmentName { get; set; }
public string Credits { get; set; }
}
// Create the class that inherits from DbContext
// The name of this class is also used as the connection string in web.config
public class EFDbContext : DbContext
{
public DbSet<Student> Students { get; set; }
public DbSet<Enrollment> Enrollments { get; set; }
}
}
}
And in my web.config file I have the following
<add name="EFDbContext"
connectionString="Data Source=JONSNA\SQLEXP2012WT;Initial Catalog=ProfessionalSiteDb; Integrated Security=True; MultipleActiveResultSets=True"
providerName="System.Data.SqlClient"/>
within the tags.
Now time to create a controller. I right click on Controllers in the Solution Explorer, and choose to Add a new Controller.
And then
And when I click Add I get
I cant figure out how to get rid of this error. I guess as a workaround I can just type the code myself, but I'd like to know if this is a bug or something I have done wrong. In VS2012 this just worked...
I'd appreciate any help or pointers. Thanks.
You don't need the EntityModels class, See below:
namespace ProfessionalSite.Models
{
public class Student
{
public int ID { get; set; }
public string LastName { get; set; }
public string FirstMidName { get; set; }
public virtual ICollection<Enrollment> Enrollments { get; set; }
}
public class Enrollment
{
public int ID { get; set; }
public string EnrollmentName { get; set; }
public string Credits { get; set; }
}
// Create the class that inherits from DbContext
// The name of this class is also used as the connection string in web.config
public class EFDbContext : DbContext
{
public DbSet<Student> Students { get; set; }
public DbSet<Enrollment> Enrollments { get; set; }
}
}
Then when you create a controller, just select the Student or Enrollment for the Model class.
I was thinking that the purpose of EF DataMigrations is to preserve old data while changing the database structure. Do I think right? But when I update the database with migrations all the old data is erased.
Do I make something wrong or maybe it isn't possible to use data migrations in this kind of scenario ?
this is DbContext:
public class CodeFirstContext : DbContext
{
public CodeFirstContext() : base ("ZenRandevuIzle")
{
}
public DbSet<User> Users { get; set; }
public DbSet<Role> Roles { get; set; }
public DbSet<Takvim> Takvims { get; set; }
public DbSet<Musteri> Musteris { get; set; }
public DbSet<Randevu> Randevus { get; set; }
public DbSet<SeansTuru> SeansTurus { get; set; }
public DbSet<Hizmet> Hizmets { get; set; }
public DbSet<Islem> Islems { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Conventions.Remove<IncludeMetadataConvention>();
}
}
this is Global.asax:
Database.SetInitializer<CodeFirstContext>(new CodeFirstContextInit());
this is migration Config:
internal sealed class Configuration : DbMigrationsConfiguration<CodeFirstContext>
{
public Configuration()
{
AutomaticMigrationsEnabled = false;
}
}
Does the CodeFirstContextInit initialiser drop the database and recreate it (what is its base class?) normally I wouldn't have an initializer when using migrations.
To use EF 4.3 Code First Data Migrations with an existing database you need to create an "empty" migration so that the Migration-History table is created and a baseline can be established.
The Steps outlined in this post should help http://thedatafarm.com/data-access/using-ef-4-3-code-first-migrations-with-an-existing-database