This is a migrated thread and some comments may be shown as answers.

Fluent API - Mapped View

1 Answer 93 Views
Development (API, general questions)
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Thomas Weidman
Top achievements
Rank 1
Thomas Weidman asked on 01 Jun 2012, 04:24 PM
Hello,

I'm using the Fluent API (with MS SQL) and am attempting to map an entity to a view.  The view is already created so when I run the CreateUpdateDDLScript via the schema handler, it attempts to create a table with the name of view.  Is there a way to prevent this from happening?

My mapping configuration looks like this:

public static MappingConfiguration<Account> CreateConfiguration()
    {
      var mapping = new MappingConfiguration<Account>();
      mapping.MapType(x => new
      {
        ActID = x.Id,
        ActTypeID = x.AccountType,
        SrvTypeID = x.ServiceType,
        ActExpDate = x.ExpirationDate,
        ActExpTypeID = x.ExpirationTypeId,
        ActApprovalState = x.ApprovalState,
        ActStatus = x.Status,
        ActLastMod = x.LastModified,
        x.Name,
        x.City,
        x.State
      }).ToTable("v_Accounts");
 
        mapping.HasProperty(x => x.Id).IsIdentity(KeyGenerator.Default);
       
      return mapping;
    }

I thought about adding a custom bit of code to remove the create table SQL from the generated ddl script but that hack wouldn't be a good long term solution.

Thanks,
Thomas

1 Answer, 1 is accepted

Sort by
0
Accepted
PetarP
Telerik team
answered on 06 Jun 2012, 03:47 PM
Hi Thomas,

Basically the fluent code generation is aimed at forward mapping scenarios and since OpenAccess does not forward map views this is an option that is missing from the fluent mapping. The easiest and the more correct way would be to override the  CreateModel method in your FluentMetdataSource. There you can get the container that got produced from your mapping. In this container you will find a collection named PersistentTypes. You will need to query that collection and find the type that corresponds to your table (by name would be most suitable I believe). Once you have obtained that type (an instance of type MetaPersistentType) you will have to set its property ShouldUpdateSchema to false.
Your code should look something like this:

protected override MetadataContainer CreateModel()
       {
           MetadataContainer container = base.CreateModel();
           MetaPersistentType accountType = container.PersistentTypes.First(x => string.Equals("Account", x.Name));
           accountType.ShouldUpdateSchema = false;
           return container;
       }

Kind regards,
Petar
the Telerik team
Follow @OpenAccessORM Twitter channel to be the first one to get the latest updates on new releases, tips and tricks and sneak peeks at our product labs!
Tags
Development (API, general questions)
Asked by
Thomas Weidman
Top achievements
Rank 1
Answers by
PetarP
Telerik team
Share this question
or