Fluent API and database views

4 posts, 0 answers
  1. Marko Gluhak
    Marko Gluhak avatar
    68 posts
    Member since:
    Apr 2006

    Posted 22 Oct 2015 Link to this post

    I "upgrade" my project from database first to Fluent and now I have problems with update DB script that wants to CREATE TABLE for all views I have in my application.

    This is parts of code for that view.

    ...
    MappingConfiguration<cViewAgeGroupExtended> cviewagegroupextendedConfiguration = this.GetcViewAgeGroupExtendedMappingConfiguration(); mappingConfigurations.Add(cviewagegroupextendedConfiguration);
    ...

    ​public MappingConfiguration<cViewAgeGroupExtended> GetcViewAgeGroupExtendedMappingConfiguration() { MappingConfiguration<cViewAgeGroupExtended> configuration = this.GetcViewAgeGroupExtendedClassConfiguration(); this.PreparecViewAgeGroupExtendedPropertyConfigurations(configuration); return configuration; }

     

     public MappingConfiguration<cViewAgeGroupExtended> GetcViewAgeGroupExtendedClassConfiguration() { MappingConfiguration<cViewAgeGroupExtended> configuration = new MappingConfiguration<cViewAgeGroupExtended>(); configuration.MapType(x => new { }).WithConcurencyControl(OptimisticConcurrencyControlStrategy.Changed).ToTable("View_AgeGroupExtended"); return configuration; }

     

    public void PreparecViewAgeGroupExtendedPropertyConfigurations(MappingConfiguration<cViewAgeGroupExtended> configuration) {             configuration.HasProperty(x => x.IdDisciplineGroup).HasFieldName("_idDisciplineGroup").WithDataAccessKind(DataAccessKind.ReadWrite).ToColumn("IdDisciplineGroup").IsNotNullable().HasColumnType("int").HasPrecision(0).HasScale(0);             configuration.HasProperty(x => x.IdTournament).HasFieldName("_idTournament").WithDataAccessKind(DataAccessKind.ReadWrite).ToColumn("IdTournament").IsNotNullable().HasColumnType("int").HasPrecision(0).HasScale(0);             configuration.HasProperty(x => x.DisciplineGroupDesignation).HasFieldName("_disciplineGroupDesignation").WithDataAccessKind(DataAccessKind.ReadWrite).ToColumn("DisciplineGroupDesignation").IsNotNullable().HasColumnType("nvarchar").HasLength(50);             configuration.HasProperty(x => x.DisciplineGroupDefaultName).HasFieldName("_disciplineGroupDefaultName").WithDataAccessKind(DataAccessKind.ReadWrite).ToColumn("DisciplineGroupDefaultName").IsNotNullable().HasColumnType("nvarchar").HasLength(255);             configuration.HasProperty(x => x.DisciplineGroupDisplayOrder).HasFieldName("_disciplineGroupDisplayOrder").WithDataAccessKind(DataAccessKind.ReadWrite).ToColumn("DisciplineGroupDisplayOrder").IsNotNullable().HasColumnType("int").HasPrecision(0).HasScale(0);             configuration.HasProperty(x => x.IdAgeGroup).IsIdentity().HasFieldName("_idAgeGroup").WithDataAccessKind(DataAccessKind.ReadWrite).ToColumn("IdAgeGroup").IsNotNullable().HasColumnType("int").HasPrecision(0).HasScale(0);             configuration.HasProperty(x => x.Designation).HasFieldName("_designation").WithDataAccessKind(DataAccessKind.ReadWrite).ToColumn("Designation").IsNotNullable().HasColumnType("varchar").HasLength(50);             configuration.HasProperty(x => x.DefaultName).HasFieldName("_defaultName").WithDataAccessKind(DataAccessKind.ReadWrite).ToColumn("DefaultName").IsNotNullable().HasColumnType("nvarchar").HasLength(255);             configuration.HasProperty(x => x.IsMale).HasFieldName("_isMale").WithDataAccessKind(DataAccessKind.ReadWrite).ToColumn("IsMale").IsNotNullable().HasColumnType("bit").HasPrecision(0).HasScale(0);             configuration.HasProperty(x => x.DisplayOrder).HasFieldName("_displayOrder").WithDataAccessKind(DataAccessKind.ReadWrite).ToColumn("DisplayOrder").IsNotNullable().HasColumnType("int").HasPrecision(0).HasScale(0);             configuration.HasProperty(x => x.ReportGroupingNumber).HasFieldName("_reportGroupingNumber").WithDataAccessKind(DataAccessKind.ReadWrite).ToColumn("ReportGroupingNumber").IsNotNullable().HasColumnType("int").HasPrecision(0).HasScale(0);             configuration.HasProperty(x => x.ReportGroupingDisplayOrder).HasFieldName("_reportGroupingDisplayOrder").WithDataAccessKind(DataAccessKind.ReadWrite).ToColumn("ReportGroupingDisplayOrder").IsNotNullable().HasColumnType("int").HasPrecision(0).HasScale(0);             configuration.HasProperty(x => x.FightDuration).HasFieldName("_fightDuration").WithDataAccessKind(DataAccessKind.ReadWrite).ToColumn("FightDuration").IsNotNullable().HasColumnType("int").HasPrecision(0).HasScale(0);             configuration.HasProperty(x => x.IdStatsCategory).HasFieldName("_idStatsCategory").WithDataAccessKind(DataAccessKind.ReadWrite).ToColumn("IdStatsCategory").IsNotNullable().HasColumnType("int").HasPrecision(0).HasScale(0); configuration.HasProperty(x => x.MinCompetitorDTBirthday).HasFieldName("_minCompetitorDTBirthday").WithDataAccessKind(DataAccessKind.ReadWrite).ToColumn("MinCompetitorDTBirthday").IsNullable().HasColumnType("date"); configuration.HasProperty(x => x.MaxCompetitorDTBirthday).HasFieldName("_maxCompetitorDTBirthday").WithDataAccessKind(DataAccessKind.ReadWrite).ToColumn("MaxCompetitorDTBirthday").IsNullable().HasColumnType("date"); }​

    When I run application I get error that table View_AgeGroupExtended already exists!

     

  2. Ralph Waldenmaier
    Admin
    Ralph Waldenmaier avatar
    202 posts

    Posted 26 Oct 2015 Link to this post

    Hi Marko,
    Thank you for contacting us.
    You can avoid the creation of view by specifying the views to not participate in the schema update mechanism. See the following snippet based on the FluentSample-NuGet where I have additionally created a view in the database named ProductView.

    MappingConfiguration<ProductView> productViewConfiguration = new MappingConfiguration<ProductView>();
    productViewConfiguration.MapType(x => new
    {
        ID = x.ID,
        ProductName = x.ProductName
    })
    .UpdateSchema(false)
    .ToTable("ProductView");           
    productViewConfiguration.HasProperty(x => x.ID).IsIdentity().WithDataAccessKind(DataAccessKind.ReadOnly);
    productViewConfiguration.HasProperty(x => x.ProductName).IsIdentity().WithDataAccessKind(DataAccessKind.ReadOnly);

    or using your example


    public MappingConfiguration<cViewAgeGroupExtended> GetcViewAgeGroupExtendedClassConfiguration()
    {
        MappingConfiguration<cViewAgeGroupExtended> configuration = new MappingConfiguration<cViewAgeGroupExtended>();
        configuration.MapType(x => new { })
            .UpdateSchema(false)
            .WithConcurencyControl(OptimisticConcurrencyControlStrategy.Changed)
            .ToTable("View_AgeGroupExtended"); return configuration;
    }

    Hence the UpdateSchema(false) tells the configuration to not maintain the objects in the database using the schema update tools.

    I hope this information is helpful for you.
    Do come back in case you have any other question.


    Regards,
    Ralph Waldenmaier
    Telerik
     
    Check out the latest announcement about Telerik Data Access vNext as a powerful framework able to solve core development problems.
  3. DevCraft banner
  4. Marko Gluhak
    Marko Gluhak avatar
    68 posts
    Member since:
    Apr 2006

    Posted 26 Oct 2015 in reply to Ralph Waldenmaier Link to this post

    Hi,

     

    thank for your solution. If I'm correct it means that for views I have to update code and database object manually? Basically if I understand this, only updatable object from code in DB is Table?

     

    Thank you
    Marko

  5. Ralph Waldenmaier
    Admin
    Ralph Waldenmaier avatar
    202 posts

    Posted 27 Oct 2015 Link to this post

    Hi Marko,
    Yes, Views are not managed by Telerik Data Access. They are readonly objects and you need to specify the exclusion for the schema updates as pointed out in my previous message. The same applies to any custom stored procedures, in case you want to use them.

    Hope that helps.
    Fell free to ask in case you have any other question.

    Regards,
    Ralph Waldenmaier
    Telerik
     
    Check out the latest announcement about Telerik Data Access vNext as a powerful framework able to solve core development problems.
Back to Top