I'm following this tutorial to add profile picture once account create.
that edit has following snippet,
Student student = db.Students.Include(s => s.Files).SingleOrDefault(s => s.ID == id);
In above example it applied to table call Student in my case I want to go with AspNetUser table , but then I have to go with UserManger feature , Once I try to Include a file then it popping that error in compile time.
But in my scenario I'm trying to include in AspNetUser or UserManager
So to populate uploaded picture. I need include following code in user manager
var user = await UserManager.Include(s => s.Files).FindByIdAsync(userid);
but then getting following error
'ApplicationUserManager' does not contain a definition for 'Include'
and no extension method 'Include' accepting a first argument of type
'ApplicationUserManager'
How to ignore and include files
EDIT:
These are the changes I've done in Model classes
File
public class File
{
public int FileId { get; set; }
..
public virtual ApplicationUser UserManager { get; set; }
}
FilePath
public class FilePath
{
public int FilePathId { get; set; }
..
public virtual ApplicationUser UserManager { get; set; }
}
ApplicationUser
public class ApplicationUser : IdentityUser
{
....
public virtual ICollection<File> Files { get; set; }
public virtual ICollection<FilePath> FilePaths { get; set; }
public async Task<ClaimsIdentity> GenerateUserIdentityAsync(UserManager<ApplicationUser> manager)
{
....
}
}
AspNetUser
public partial class AspNetUser
{
public AspNetUser()
{
..
}
....
public virtual ICollection<File> Files { get; set; }
public virtual ICollection<FilePath> FilePaths { get; set; }
}
then try to add migration to the project by typing the following into the PMC:
add-migration File
add-migration FilePaths
then getting following error in console
No migrations configuration type was found in the assembly
'Project_Name'. (In Visual Studio you can use the Enable-Migrations
command from Package Manager Console to add a migrations
configuration).
then I typed the following into the PMC:
Enable-Migrations project_name.Models.sampleEntityFrameworkEntities
then I got following error in console
Creating a DbModelBuilder or writing the EDMX from a DbContext created
using Database First or Model First is not supported. EDMX can only be
obtained from a Code First DbContext created without using an existing
DbCompiledModel.
then I try to migrate Files and FilesPath using following code
add-migration File
add-migration FilePaths
Get error in console
Creating a DbModelBuilder or writing the EDMX from a DbContext created
using Database First or Model First is not supported. EDMX can only be
obtained from a Code First DbContext created without using an existing
DbCompiledModel.
If I understood you correctly you're trying to follow the guide but using ASP.NET Identity. ASP.NET Identity is a unique system with its own classes and DBContext. In order to add new fields to the user you need to modify ApplicationUser class. Just add your property to this class and apply migration if needed. This will add a new column to the AspNetUser. Then add your field to the ViewModel (if any) and to other logic that is required to work with your field.
Related
I have a Table "IncomingChecks" in my database. I've created it using EF Code first. Now, I've added a view to my database based on this table named "ViewIncomingChecks" using Sql Server Management Studio and I want to use its data in my app using Entity Framework.
I copied the model class and changed its name and added it to the context:
public class ViewIncomingCheck
{
[Key]
public int Id { get; set; }
//...
}
public class CheckDataContext : DbContext
{
public virtual DbSet<ViewIncomingCheck> ViewIncomingChecks { get; set; }
//...
}
now when I run the app, it throws an exception saying the DB Context has been changed and needs a migration. I even tried to add a migration (which seems to be the wrong option) and when I add the migration, it says that the object ViewIncomingChecks is already in the database.
How can I use this view in my code?
Edit
My current solution is to have another context just for the views. This way it doesn't conflict with the EF Migrations. Is this the best option or is there a better way to deal with it.
According to what I have done in my project:
First add public virtual DbSet<ViewIncomingCheck> ViewIncomingChecks
{ get; set; } to your DbConext
Now create a migration something called ViewDbSetAdded
Remove all the code from the both Up and Down method and it will look like as follows:
Migration Code:
public partial class ViewDbSetAdded : DbMigration
{
public override void Up()
{
}
public override void Down()
{
}
}
Now run update-database command and it will run an empty migration.
I'd like to use EF code first approach. I added the database and I generate the tables . Then I added this class
public class Invitation
{
[Key]
public int Id { get; set; }
[DefaultValue(false)]
public bool State { get; set; }
public string Mail { get; set; }
public string Tel { get; set; }
public string Name { get; set; }
public string Qr_code { get; set; }
}
I run these command then :
add-migrations second
update-database
the Up and Down methods of the second class migration are empty!! and no table is added to the database.
The context
public class ApplicationContext: IdentityDbContext<ApplicationUser>
{
public ApplicationContext()
:base("DefaultConnection")
{
Database.SetInitializer<ApplicationContext>(new CreateDatabaseIfNotExists<ApplicationContext>());
}
public static ApplicationContext Create()
{
return new ApplicationContext();
}
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
}
}
So I need to know
What is the reason of this problem?
How can I fix it?
Looks like you forgot to tell Entity Framework about the new table that you want added (DbSet<Invitation>)
Once you add this, Entity Framework should add the table(s) you want added in the Migration script, respectively.
In summation, you would need to add this line :
public DbSet<Invitation> Invitations { get; set; }
and/or
public IDbSet<Invitation> Invitations { get; set; }
and run another Migration Script.
Try adding the following into your ApplicationContext class
public DbSet<Invitation> Invitations { get; set; }
Then running;
Enable-Migration
Add-Migration note_of_changes
Update-Database
I think you need to create an initial migration. If this is your first migration (note that this will clear your existing migration history so only use if you're happy to discard your existing migration history)
Delete your Migrations folder in the solution
Remove your changes (remove the reference to the new table from your DbContext). Note that -IgnoreChanges could well make this step redundant but I can't say for certain.
Remove the MigrationHistory table from your database (it most likely won't exist but you can go ahead and delete it if it is)
Now enable migrations (in package manager console)
Enable-Migrations
Then create your initial migration. This will create a migration matching your existing schema with empty methods
Add-Migration Initial –IgnoreChanges
Update-Database
Then update your DbContext with your new table reference and make any other changes you need to and do
Add-Migration MyChanges
Update-Database
That should apply the changes to the database. Some more info over at MSDN if you need it.
I am creating a ASP.Net MVC project in Visual Studio Community 2015 where I have to enable users to create and manage advertisements. This is a one-to-many relationship meaning that a user may create as many advertisements as he pleases. Every user can see all advertisements but only the ones that created them can edit them. Since the MVC template already comes with Identity implemented, I want to use the class ApplicationUser as my user class.
I added a class Avertisement to my Model folder that looks like this:
public class Advertisement
{
public int AdvertisementId { get; set; }
public string Description { get; set; }
public string ApplicationUserID { get; set; }
public virtual ApplicationUser ApplicationUser { get; set; }
}
Then I added the Advertisement DbSet to the ApplicationDbContext class:
public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
public ApplicationDbContext() : base("DefaultConnection", throwIfV1Schema: false) {}
public DbSet<Advertisement> Advertisements { get; set; }
public static ApplicationDbContext Create()
{
return new ApplicationDbContext();
}
}
Afterwards, I enable migrations and rebuild the solution:
Enable-Migrations
add-migrations InitialCreate
update-database
Until this point everything runs smoothly. Now comes my problem. I now try scaffolding by adding an Advertisement controller with the "MVC 5 Controller with views, using Entity Framework" template but the generated controller comes with the following error that appears everytime db.ApplicationUsers appears on the controller. I have no idea what I am doing wrong and I've looked everywhere on stackoverflow and the internet but none of the solutions worked for me. What do you think could be wrong?
First you need to add the link between ApplicationUser and Advertisment, find the ApplicationUser class and add this:
public class ApplicationUser : IdentityUser
{
// Add this line
// vvvvvvvvvvvvv
public virtual ICollection<Advertisment> Advertisments { get; set; }
}
In your action method, you access the users with this:
db.Users
ApplicationUser is just a class Identity creates for you. The actual table where Identity stores the user data is AspNetUsers, so
db.AspNetUsers
should work for you.
I have made a project in which i have choose MVC + Web Api template with Authorized attribute.I have made my SQL Data base on Windows Azure and publish my project successfully.I can see the data of the registered user in users table.
Now i wanted to add one more table to my Database but i am not getting how to do it.I know i have to make a model of that table type and update the database.But i don't getting how to write this part in code.I am total newbie in this part.
I have seen Account controller class it is looking just an alien to me :)
can somebody help me.
Step 1: Create the model for your table
public class Comment
{
public int ID { get; set; }
public string Content { get; set; }
public DateTime DateCreated { get; set; }
public virtual ApplicationUser Author { get; set; }
}
Step2: Add a dbSet for this model in your DbContext class
public class DDBContext : DbContext
{
public DDBContext()
{
/* ... */
}
// add the DbSet
public DbSet<Comment> Comments { get; set; }
//... additional models ommitted
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
Database.SetInitializer(new MigrateDatabaseToLatestVersion<DDBContext, Configuration>());
}
}
Step 3: Enable Migrations
In the packagemanager console issue those commands:
enable-migrations
add-migration MyNewTableAdded
Step 4: Build the project
Step 5: Issue the update-database command in the packagemanager console
There is a detailed explanation on www.asp.net including the process for Azure. Click here!
I am trying to implement Entity Framework using code first but it is not building the database tables. I'm not sure what i'm doing wrong. I am using this tutorial -http://msdn.microsoft.com/en-us/data/jj193542 but I am trying to use it with a web application instead of a console application. I created a Models folder and put my context class in that folder. Here is my dbcontext code:
namespace GroceryAssistant.Model
{
public class GroceryShopperContext : DbContext
{
public DbSet<List> Lists { get; set; }
public DbSet<ListItem> ListItems { get; set; }
public DbSet<User> User { get; set; }
}
}
I created put the connection string in the web.release.config and the web.debug.config files. I'm not sure what else I have to do. Where should i put my DbContext code so that the tables will be created?
If you do not change the default behavior, the first time you use the DbContext it will create your database.