0

Ihave a DB First EF5 project that I am implementing SimpleMembership in. I have most of it working, but a question has come up.

The main User table created by Simple Membership has the UserName in it. I have a couple other places in the app where I need to query this table, specifically the userName. Simple Membership does not use the Data.EntityClient in the connection string, so i have it set to the SqlClient.

So because i don't have an entity model with the provider User table, I am not sure how to query it. Usually i would create an instance of the entity model and use LINQ on it, but when I try it I get a very long winded error about mixing code first with entity first. I have modified the 'initialSimpleMembershipAttribute' so it points to the separate connection string I made for the Membership tables.

One solution i thought of was to save the user, then copy the username to one of my custom tables, then i can query it through EF, but this seems like it would violate some kind database 'best practice' of duplicated data.

Another idea I had is to create a second edmx model for the tables that Membership created, but if SimpleMembership does not use the EntityClient, does that also mean it will not recognize a entity model?

BattlFrog
  • 3,370
  • 8
  • 56
  • 86

1 Answers1

0

You're way over complicating things.

If you have your own model, then you can just get rid of the model that the default template gives you, and use yours instead. It doesn't matter if it's Code first or database first or whatever. Just change the Initialize SimpleMembershipAttribute to remove the references to the default model they give you, and make sure you modify the InitializeDatabaseConnection call correctly.

WebSecurity.InitializeDatabaseConnection("YourConnection", "WhateverYouCallYourUserTable", 
    "WhateverYourUserIdIs", "WhateverYourUserNameColumnIs", autoCreateTables: true);
Erik Funkenbusch
  • 92,674
  • 28
  • 195
  • 291
  • Ok I can do that. But I still need the separat connection strings, right? – BattlFrog May 12 '13 at 22:42
  • @teahou - I don't understand what you mean by "separate connection strings". If you have your own database and model, you only have the one connection string. – Erik Funkenbusch May 12 '13 at 23:33
  • One for the simplemembership tables, which uses the System.Data.SqlClient provider, and one for the entity model, which contians all my other tables and uses the System.Data.EntityClient. This is per this post: http://stackoverflow.com/questions/12575311/using-simplemembership-with-ef-model-first. My original thinking was as you have stated, that it should not be so hard. Please let me know how I can simplify this so called simple membership. – BattlFrog May 13 '13 at 15:09
  • @teahou - Ahh.. yes, you just have two connection strings that point to the same database. – Erik Funkenbusch May 13 '13 at 15:24
  • So that takes us back to the original question, am I supposed to have 2 edmx models as well? If not, how do I query the tables using the system.data.sqlclient connection? – BattlFrog May 13 '13 at 16:35