Entity Framework and empty migrations class - c#

In my solution I have two projects: SharePoint and Class Library. In ClassLibrary is a class DBContext and models. "Enable-Migration" generated class Configuration.cs. Next, I created the first migration through the "Add-Migration Initialize" and it worked. My next step was to "Update-Database" everything was fine. But after a while I changed the model and I wanted to generate migration. Unfortunately, I get a blank method Up and Down. Entity Framework does not see my changes ... The models are written without annotations and Context it inherits DbContext and is written like this:
public class SchoolContext: DbContext
{
public SchoolContext (): base () {}
public DbSet <Student> Students;
}
Models are written in this way:
public class Student
{
public int FirstName {get; set; }
}

Related

How to save code in the model classes of mvc entity framework even the classes deleted?

Sometimes when I update in the database, and go update the model from database EntityFramework classes gets deleted after update, I searched a lot for a solution but I found that I have to delete edmx file and create it again.
I do that and the problem solved but the code I wrote in the model classes removed like the validation code, is there anyway can I keep the code I wrote even I delete and recreate edmx file ?
EntityFramework will rebuild/generate all of the classes every time you change something.
If I got your problem right, I suggest using metadata classes:
public class EntityModel{
public int Test {get; set;}
}
public class EntityModel2{
public int Test2 {get; set;}
}
Then create 2 new files: Metadata.cs and PartialClasses.cs
Metadata.cs
public class Metadata{
public class EntityModelMetadata {
[JsonIgnore] //or whatever you need
public int Test {get; set;}
}
public class EntityModel2Metadata {
[XmlIgnore] //or whatever you need
public int Test2 {get; set;}
}
}
PartialClasses.cs
namespace YourNamespace{
[MetadataType(typeof(EntityModelMetadata))]
public partial class EntityModel {}
[MetadataType(typeof(EntityModel2Metadata))]
public partial class EntityModel2 {}
}
You need to create _Metadata class for each model you want to change and than you need to connect metadata class with EntityFramework class in PartialClasses.cs in the same way like shown.

C# Common DbContext for ASP.NET Identity with webforms

I'm beginner to .NET but I'm fairly familiar with building webapplications using 3 tier architecture. I usually have a webapplication project for the webforms and a wpf application project to keep my Domain, Controller and Data Access Layer classes.
I'm familiar with creating a DbContext class and using it to deal with the database through EF. I've added .NET Identity to my webapplication project but I need to make changes to the identity user and add more data to it.
I'm following this tutorial to accomplish this but it is intended for MVC. I have 2 seperate context files at the moment, one for identity and other for my usual purposes. is there a way I can have one context?
my DbContext looks like,
namespace ExammerCore.Infrastructure
{
class ExammerContext : DbContext
{
public ExammerContext()
: base("DefaultConnection")
{
}
}
}
the identity DbContext I made using the tutorial looks like,
namespace ExammerCore.Infrastructure
{
class IdentityContext : IdentityDbContext<ApplicationUser>
{
public IdentityContext()
: base("DefaultConnection")
{
}
}
}
My IdentityModels.cs looks like,
namespace ExammerCore.Domain
{
public class ApplicationUser : IdentityUser
{
//You can extend this class by adding additional fields like Birthday
public string BirthDate { get; set; }
}
}
how can I blend these two together to have one DbContext class? or is there another way to add custom fields to a user without inheriting Identity classes?
My solution structure looks like this,
Solution structure
I've googled a lot haven't found an answer.
Change the line
class ExammerContext : DbContext
to
class ExammerContext : IdentityDbContext<ApplicationUser>
to pull all the Identity DbSets into your ExammerContext
in your dbcontext you should have properties that represent what entities get used to build tables.... that should look something like this...
class ExammerContext : DbContext
{
public IDbSet<ApplicationUser> ApplicationUsers { get; set; }
// add other classes/tables here.
public ExammerContext()
: base("DefaultConnection")
{
}
}
this way you will only need your context.

Add a non mapped property to entity EF5

I'm using EF5 to produce a model from an existing DB structure. I map Insert/Update/Delete Stored Procedures to the entities. This is working fine.
What I would like to do next is pass a UserId to these SPs as a parameter but not have the UserId as a column within the underlying table (the SPs will utilize this parameter). I have lots of entities. Is it possible to somehow add a property that will always be added back in even after updating the model from the DB?
Many Thanks
If you are using EDMX to generate the Entity Framework object model, your classes are partial and you can add properties to the partial classes of your entities which will survive database regeneration.
If you have a ParticularEntity table in the DB and referenced in the EDMX, you may add a partial class file ParticularEntity.Augments.cs (the name is for your reference, and you can have multiples as normal with partial classes) to your project and within it
public partial class ParticularEntity
{
public string UserId { get; set; }
public void DoSomething(string userId)
{
someFunctionThatYouWantToNotBeAnExtension();
}
}
Alternatively, you could write a set of extension methods which your maps utilize. I don't think that's as clean as extending the EF classes with partials, though.
Entity created by EF are partial class so you can extend that class with your custom properties
YourEntity.cs //created by EF
public partial class YourEntity
{
public string Name { get; set; }
...
}
YourEntityExtended.cs // created by you
public partial class YourEntity
{
public int Id { get; set; }
}

Entity Framework multiple projects with code first migrations

I have a class library project named "Core" which defines CoreContext with properties for common classes:
public class CoreContext : DbContext
{
public DbSet<User> Users { get; set; }
public DbSet<Role> Roles { get; set; }
public DbSet<UserContactInfo> UserContactInfos { get; set; }
... more common properties ...
}
Then I have multiple projects referencing the "Core" project and defining new Contexts inheriting CoreContext:
Project1:
public class Project1Context : CoreContext
{
public DbSet<Foo> Foos { get; set; } //object defined in Project1
}
Project2:
public class Project2Context : CoreContext
{
public DbSet<Bar> Bars { get; set; } //object defined in Project2
}
When I change something in Project1Context I create a new migration in Project1 which is OK,
but if I change something in the Core project, lets say add property Company to UserContactInfo I will have to go to Project1 and Project2 and add new migration and I will end up with migrations related to the Core project in Project1 and Project2.
My question is: Is there a way I can keep the migrations for CoreContext in the Core project and Project1Context and Project2Context migrations to their corresponding project and automatically execute all migrations related to the project, so when I start Project1 it will run its own migrations and CoreContext migrations?
As clarification, I have separate databases for Project1 and Project2 and recently Updated EF to version 6.
What you are saying is not possible..
Just imagine if you are using model to create basic CRUD views and then changing the model. Your question is similar..
Perhaps you could write your own sub class which inherits from MigrateDatabaseToLatestVersion?

Entity Framework Database First inherits models from base class

I am using Entity Framework Database First for working with db.
I have a base class EntityBase
public class EntityBase
{
public Int32 Id { get; set; }
}
I have some other classes that are generated by EnitytFramework and represents tables of my db, for example, "User" class:
public class User
{
public int32 Id{get;set;}
public class Name{get;set;}
public class Age{get;set;}
}
I want all EF model classes to be inherited from EntityBase:
public class User : EntityBase
I can do this manually, by deleting Id field from User class and defining inheritance, but after I am updating model from db, all manually-made changes dissapears.
Is there any way to keep or automatically add inheritance to EF model classes?
EF database first work with t4 files if you change manually the class, save and compile, T4 will generate the classes again and you lose your changes.
You must make this change in T4 template. Search the EntityClassOpening method and change the last line:
_typeMapper.GetTypeName(entity.BaseType)
with this code:
_typeMapper.GetTypeName(entity.BaseType) ?? "EntityBaseClass"

Categories

Resources