Fluent API - Mapped View

Thread is closed for posting
2 posts, 1 answers
  1. Thomas Weidman
    Thomas Weidman avatar
    12 posts
    Member since:
    Apr 2004

    Posted 01 Jun 2012 Link to this post


    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,
            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.

  2. Answer
    PetarP avatar
    754 posts

    Posted 06 Jun 2012 Link to this post

    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,
    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!
Back to Top