vita icon indicating copy to clipboard operation
vita copied to clipboard

Existing MsSql Views in DbFirst

Open sapatag opened this issue 5 years ago • 8 comments

In Vita 1.x vdbtool scripted the existing MsSql views, and they worked as expected. How can I script/use them in 3.x?

Another question: In 1.x I used lots of [NoColumn] fields from other Entity types. 3.x does not allows it. Is it intentional? For the latter I made the folloing quick fix:

private void BuildEntityMembers() {
  foreach (var entInfo in Model.Entities) {
    bool isTable = entInfo.Kind == EntityKind.Table; 
    var props = entInfo.EntityType.GetAllProperties();
    EntityMemberInfo member; 
    foreach (var prop in props) {
      EntityMemberKind kind;
      if (TryGetMemberKind(entInfo, prop, out kind)) {
        var memberAttrs = prop.GetAllAttributes();     //Quick fix
        if (memberAttrs.Any(a=>a is NoColumnAttribute))//
          kind = EntityMemberKind.Transient;           //
        member = new EntityMemberInfo(entInfo, kind, prop); //member is added to EntityInfo.Members automatically
        member.Size = member.GetDefaultMemberSize();
        // if it is table, or prop is declared on THIS entity (not inherited), take all attributes
        if(isTable || prop.DeclaringType == entInfo.EntityType)
          member.Attributes.AddRange(memberAttrs);
        else {
          // for inherited members on views we take only OneToMany and ManyToMany attributes; 
          //  - we want to allow list properties on view entities (see vFictionBook view in Sample book store)
          var listAttrs = memberAttrs.Where(a => a is ManyToManyAttribute || a is OneToManyAttribute).ToList();
          member.Attributes.AddRange(listAttrs); 
        }
      } //else - (kind not found) - do nothing, the TryGetMemberKind should have logged the message
    }
  }//foreach entType
}

sapatag avatar Feb 20 '20 11:02 sapatag

With dbViews - so the problem is that vdbtool does not script them? I believe in general DbViews are supported, you can define them with LINQ, etc, there are unit tests for these About NoColumn - did not quite get what it means 'from other entity types'. I will look at NoColumn and what's going on there. In general, it should be just pure in-memory extra property on entity, right? - that's how you use it?

rivantsov avatar Feb 21 '20 16:02 rivantsov

Views: Vita 1.x has IsView in the Entity attribute and vdbtool scripted the db views.

NoColumn: Exactly. I use them as "Transient" regardles of their type. E.g.:

[Entity(...)] public interface IX1 {...}

[Entity(...)] public interface IX2 { ... [NoColumn] IX1 X1 {get;set;} }

With [NoColumn] IX2.X1 is not a foreign key linked record.

sapatag avatar Feb 21 '20 17:02 sapatag

ok, got it, will look at it, thanks for reporting it!

rivantsov avatar Feb 21 '20 17:02 rivantsov

My solution for NoColumn: private void BuildEntityMembers() { foreach (var entInfo in Model.Entities) { bool isTable = entInfo.Kind == EntityKind.Table; var props = entInfo.EntityType.GetAllProperties(); EntityMemberInfo member; foreach (var prop in props) { EntityMemberKind kind; if (TryGetMemberKind(entInfo, prop, out kind)) { //-------------------------- var memberAttrs = prop.GetAllAttributes(); if (memberAttrs.Any(a=>a is NoColumnAttribute)) kind = EntityMemberKind.Transient; //-------------------------- member = new EntityMemberInfo(entInfo, kind, prop); //member is added to EntityInfo.Members automatically member.Size = member.GetDefaultMemberSize(); // var memberAttrs = prop.GetAllAttributes(); ------------ moved upper // if it is table, or prop is declared on THIS entity (not inherited), take all attributes if(isTable || prop.DeclaringType == entInfo.EntityType) member.Attributes.AddRange(memberAttrs); else { // for inherited members on views we take only OneToMany and ManyToMany attributes; // - we want to allow list properties on view entities (see vFictionBook view in Sample book store) var listAttrs = memberAttrs.Where(a => a is ManyToManyAttribute || a is OneToManyAttribute).ToList(); member.Attributes.AddRange(listAttrs); } } //else - (kind not found) - do nothing, the TryGetMemberKind should have logged the message } }//foreach entType }

sapatag avatar Sep 04 '20 07:09 sapatag

thanks, I will look at it over weekend. I will be back at active VITA development soon, this Sept, and will address all issues reported, I promise

rivantsov avatar Sep 04 '20 17:09 rivantsov

NoColumn attribute should be working now. With existing dbview it's a different story, looks like they are gone, did not think they are used much. I will look a little more, and see if it's possible to bring them back

rivantsov avatar Nov 05 '20 05:11 rivantsov

Thanks for NoColumn! About views: they are very usefull in complex databases servicing rapidly changing busines requirements.

sapatag avatar Nov 05 '20 07:11 sapatag

Views - will get to them and Db1st tool shortly

rivantsov avatar Jun 18 '21 04:06 rivantsov