I work on a legacy database that stores Users in one of two tables (ActiveUsers, InactiveUsers). In my DbContext I defined the following DbSets to work with the two Usertables:
public DbSet<User> ActiveUsers{ get; set; }
public DbSet<User> InactiveUsers{ get; set; }
How can I map the User entity to the tables? I'm stuck with these Mappings:
// OK
modelBuilder.Entity<User>().ToTable("ActiveUsers");
// Overwrites the mapping above :(
modelBuilder.Entity<User>().ToTable("InactiveUsers");
I think I need to configure the DbSets somehow...
PS: This MSDN article seems to solve my problem for "classic" EF with an EDMX.
As Pawel pointed out:
CodeFirst does not support MEST
Related
I'm learning ASP.NET Core and I'm having some problems with the following scenario:
I created an extension class for IdentityUser provided by Microsoft.AspNetCore.Identity, the extension class add some extra field to the default database AspNetUsers:
public class ApplicationUser : IdentityUser
{
public string FirstName { get; set; }
public string LastName { get; set; }
public DateTime BirthDate { get; set; }
public string LockoutMessage { get; set; }
public string SessionId { get; set; }
}
I was able to update the table structure executing the following commands:
add-migration <migration name> -context <app context>
update-database
Problem
Suppose now I used the software Microsoft SQL Server Management Studio for create another table called UserDetails which have as FK the id of the AspNetUsers table.
I want generate the class inside the Models folder with all the properties from my application, so I don't need to write manually the property of the table of the new table, how can I do that?
I tried: update-database but not seems to work.
The only way to bring in stuff from a database is with Scaffold-DbContext. However, that's an all or nothing affair. It's going to create entity classes for every table in the database (regardless of whether they already exist) and a DbContext to boot.
Either you're using code first and you create your entities and generate migrations that you run against the database OR you make changes to the database and then use the Scaffold-DbContext command to generate the context and all the associated entities. You cannot mix and match.
Long and short, you need to pick a strategy and stick with it. If you're more comfortable with the database then do everything there and scaffold the code from that. Otherwise, if you want to use code first, then make a commitment to that and never manually touch your database.
I am having trouble including my Identity User in query results. Other entities are included just fine, no matter how many levels deep.
Here's the model I'm using.
Building * --- 1 City
* *
| /
| /
1 1
ApplicationUser
And the context:
public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
public DbSet<City> Cities { get; set; }
public DbSet<Building> Buildings { get; set; }
}
Both Building and City have these properties:
public Guid ApplicationUserId { get; set; }
public virtual ApplicationUser ApplicationUser { get; set; }
The query I'm using to retrieve data:
var building = context.Buildings
.Include(c => c.ApplicationUser)
.Include(c => c.City)
.Include(c => c.City.ApplicationUser)
.First();
In the result City is populated just fine, however ApplicationUser is not.
Could this be a naming issue? I've already tried UserId / User and AspNetUserId / AspNetUser as property names without succes.
I'm using Migrations to create the database. The table name that gets created for users is AspNetUsers.
I'm using entity framework 7 beta 7, not sure if this applies to other versions as well.
These are the files generated by EF Migrations.
ApplicationDbContextModelSnapshot.cs
20150929181902_Init.cs
20150929181902_Init.Designer.cs
(I noticed I forgot to include the Building > City relation when generating files for upload, but that shouldn't matter for the example)
I've finally figured it out.
ApplicationUser inherits from IdentityUser which inherits from IdentityUser<string>. The generic type is used as the primary key, so a string!
I changed my Guid foreign key properties to strings and now everything is working.
Just to help anyone who also have a similar issue. I was facing same thing, same scenario: My custom identity user has some related entities and using .Include was not working. None of them would come back. But they used to.
Turns out I had created a custom ApplicationUserStore to automatically include the entities I would normally always need when retrieving a User but I had only overridden FindByIdAsync method. When I tried to use FindByNameAsync no entity would come back, obviously. Just had to override this and the other Find methods.
I want to add 2 Extra fields in UserProfile Asp.Net Simple membership, But unable to add and not find any help from internet. Please give me some solutions.
Thanks in advance
To add fields to UserProfile in SimpleMembership (I will assume you want to use migrations and EF):
Ensure you have enabled migrations (use the Enable-Migration in the package manager console)
Edit the UserProfile class (assuming you are using, for example, the Visual Studio 2012 -> New Project -> ASP.NET MVC 4 Web Application -> Internet Application template, this will be created for you) to add your new properties (example below)
Add a migration which will update your database using the Add-Migration command.
Optionally edit the generated migration to handle default values and non-nullable fields.
Run that migration against your development database using the Update-Database command.
I have provided an overview about how SimpleMembership works, with UserProfile in this answer. Note that UserProfile can be moved from the UsersContext to the same DbContext as all your other classes, it does not have to be in a separate context.
SimpleMembership is designed to play well with code-first EF development, so that is the process I have outlined above. An example updated UserProfile class with a Forename, Surname and a LoginCount field would look like:
[Table("UserProfile")]
public class UserProfile
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int UserId { get; set; }
public string UserName { get; set; }
[MaxLength(40)]
[Required]
public string Forename { get; set; }
[MaxLength(40)]
[Required]
public string Surname { get; set; }
[Required]
public int LoginCount { get; set; }
}
References:
ASP.NET MVC 4 Entity Framework Scaffolding and Migrations - task 3 shows how to enable migrations
MSDN: Code First Migrations - everything you need to know about migrations
StackOverflow: How do I use my own database with SimpleMembership and WebSecurity? What is MVC4 security all about?
I've been struggling to try and get Data Services to work with the new LightSwitch 2.0 OData Data Source.
Noticing that OData 3.0 is still not supported I had to fall back to 2.0 version.
It happens that my Data Context is nothing but wrapper over some xml serialized object to enable OData access.
This "magic" happens using the Reflection Provider and it works fine regarding all the CRUD operations.
The problems start when I try to use this service in LightSwitch, and realize that all my entity associations are wrong.
The situation I have is exactly the same as if you look at Microsoft's sample code.
So, using this data model:
[DataServiceKeyAttribute("OrderId")]
public class Order
{
public int OrderId { get; set; }
public string Customer { get; set; }
public IList<Item> Items { get; set; }
}
[DataServiceKeyAttribute("Product")]
public class Item
{
public string Product { get; set; }
public int Quantity { get; set; }
}
It's obvious the "one-to-many" relationship between Order [1 - *] Items.
But looking at the xml metadata of this service, the association is declared as "many-to-many":
<Association Name="Order_Items">
<End Type="WEBfactory.StreamInsight.Adapters.Carel.DataServices.Order" Multiplicity="*" Role="Order"/>
<End Type="WEBfactory.StreamInsight.Adapters.Carel.DataServices.Item" Multiplicity="*" Role="Items"/>
</Association>
Now, this doesn't really bother much when using a "Service Reference" client, but since LightSwitch doesn't support "many-to-many" relationships I always get a warning when trying consume this service and the relations are neither imported, nor possible to manually define.
Does anyone have a clue how to work enforce a relationship type using the Reflection Provider?
Thanks!!
The relationship between Order and Item in this case actually is many:many - if it were 1:many as you propose, an Item could only be in one Order.
That said, you can create the 1:* relationship by adding a corresponding property to the Item class:
[DataServiceKeyAttribute("Product")]
public class Item
{
public string Product { get; set; }
public int Quantity { get; set; }
public Order Order { get; set; }
}
That results in the following $metadata, which may or may not cause the same problem:
<Association Name="Order_Items">
<End Type="Scratch.Web.Order" Multiplicity="*" Role="Order"/>
<End Type="Scratch.Web.Item" Multiplicity="*" Role="Items"/>
</Association>
<Association Name="Item_Order">
<End Type="Scratch.Web.Order" Multiplicity="0..1" Role="Order"/>
<End Type="Scratch.Web.Item" Multiplicity="*" Role="Item"/>
</Association>
This is likely a limitation of the Reflection provider (I'll edit this answer if it turns out not to be), so the only workarounds today are to use either the EF provider or a custom provider.
I would make my WCF RIA Service work as described here:
How to Create a Many-to-Many Relationship
http://blogs.msdn.com/b/lightswitch/archive/2010/12/16/how-to-create-a-many-to-many-relationship-andy-kung.aspx
I'm using ASP.NET MVC 3 and my repositories are using ADO.NET with stored procedures to fill my domain objects. I'm not 100% sure where certain objects should be filled. For example I need to fill the User property based on the UserId retrieved. What is the best approach to do this? For each domain object I have a repository and a unit of work object that manages the transaction and connection.
public class Comment
{
public string Post { get; set; }
public int UserId { get; set; }
public int ParentCommentId { get; set; }
List<Comment> Comments { get; set; }
public User User { get; set; }
}
Any insight would be greatly appreciated.
Without knowing more about your domain it is difficult to answer the quesiton. This is more related to Domain Driven Design than to the MVC framework. The concept you need to investigate is called Aggregates in DDD. An aggregate is a collection of model objects that logically fit together. One of the models in the aggregate would be a root object that handles the other model objects in the aggregate including persistence.
You would have a repository for each aggregate root and not for each model object. I would recommend going through the free downloadable minibook from InfoQ called Domain Driven Design Quickly. The author has summarized the excellent Domain Driven Design book by Eric Evans in this minibook.