How to map an Entity framework model to a table name dynamically (run-time)?

Posts   
 
    
MrTouya
User
Posts: 2
Joined: 08-Mar-2011
# Posted on: 08-Mar-2011 21:46:11   

Hi Guys,

I found documentation for custom persistence mapping by overriding the “OnModelCreating” method. I am not sure on how to implement this using LLBLGen Pro.

This is the problem I am trying to solve..

When a user logs on to my application, a set of views are presented to user based on their login. My views are named like so: • 3050_CUSTOMER_VIEW • 3050_ORDERS_VIEW • 3051_CUSTOMER_VIEW • 3051_ORDERS_VIEW If customer 3050 logs in, the model should map to the 3050 views and if the customer is 3051, the underlying map should map to the 3051 views. If you use the “OnModelCreating” method you should be able to do something like the following: protected override void OnModelCreating(ModelBuilder modelBuilder) { ...

var customer = ((PORTAL_USERS)e).Cusotmer; //Not sure how to do this yet..??

switch (customer) { case 3050: modelBuilder.Entity<Generic_View>().MapSingleType().ToTable("3050_CUSTOMER_VIEW "); break; case 3051: modelBuilder.Entity<Generic_View>().MapSingleType().ToTable("3051_CUSTOMER_VIEW "); break; } }

I have a few questions:

  1. Is there an easier way to implement what I am trying to do in LLBLGen Pro?
  2. Where is the LLBLGen Pro equivelant of "OnModelCreating"?
  3. How can I implement the above in LLBLGen Pro?
  4. I have a custom Authentication Service that looks like the following:

public class AuthenticationService : IEntityLoginManager {

    public IPrincipal Login(ILoginCredential credential, EntityManager entityManager)
    {
        UserIdentity identity = null;

        if (credential == null)
        {
            throw new LoginException(LoginExceptionType.NoCredentials, "No Credentials");
        }

        if (entityManager == null)
        {
            throw new LoginException(LoginExceptionType.NoLoginManager, "No Login Manager");
        }

        var portalUserss = new EntityQuery<PORTAL_USERS>("PORTAL_USERS", entityManager);
        var userInfoData = from credentials in portalUserss where (credentials.USER_NAME == credential.UserName && credentials.PASSWORD == credential.Password) select credentials;

        if (userInfoData.Count() != 0)
            identity = new UserIdentity(credential.UserName, "Custom", true);
        else
            identity = new UserIdentity(credential.UserName, "Custom", false);


        var principal = new UserBase(identity);

        return principal;
    }

    public void Logout(IPrincipal principal, EntityManager entityManager)
    {


    }
}

How do I get the customer number to LLBLGen Pro's implementation of the "OnModelCreating"?

The example I found for the "OnModelCreating" method is here - http://weblogs.asp.net/scottgu/archive/2010/07/23/entity-framework-4-code-first-custom-database-schema-mapping.aspx

Thanks,

Stephane Touya

PS. A SMALL EXAMPLE WOULD BE GREAT!!!!

MTrinder
User
Posts: 1461
Joined: 08-Oct-2008
# Posted on: 08-Mar-2011 22:15:43   

To be honest, I think you are coming at this from the wrong way. This database design with a set of views for each customer is going to be a hugely limiting factor in your system.

Is this database design already set in stone ? Why can you not simply have a view called Customer_View and filter it at runtime based on the customer ID ?

MrTouya
User
Posts: 2
Joined: 08-Mar-2011
# Posted on: 08-Mar-2011 23:30:56   

Hi MTrinder,

Thanks for the quick response!

I am working with a customer that has a legacy database design that is extremely difficult to change. The different views calculate values such as sum_of_amountpaid based on different criteria. I do realize there are better ways of doing this, but it would require a major redesign of their in house programs, database, reporting system, etc.

Stephane

daelmo avatar
daelmo
Support Team
Posts: 8245
Joined: 28-Nov-2005
# Posted on: 09-Mar-2011 06:50:17   

That (custom mapping the table name on runtime) can't be done. So I recommend to you map all the views at LLBLGen Pro Designer. Then either you:

a. Discover the name at runtime based on the user. For example, if the user is '3059-, then the view should be 'CustomerTypedView3059'. Now with some reflection you can create the object and fill it.

b. (Better). Have a method that receive the user, then internally you, based on a 'switch' select what is the correct TypedView to instantiate and fill.

David Elizondo | LLBLGen Support Team