EF 6 - using attributes based generation

Posts   
 
    
costica
User
Posts: 2
Joined: 10-Aug-2018
# Posted on: 10-Aug-2018 00:39:38   

Hi,

I have an existing app where the EF entities have been coded manually using attributes.

Example:

   [Table("DepartmentAccess", Schema = "dbo")]
    public class DepartmentAccess
    {
        [Key, Column("ID")]
        public int DeptAccessID { get; set; }

        [Required, Column("Department")]
        public string CostCentreID { get; set; }

        [Required, Column("Mnemonic")]
        public string GivenToAdUsername { get; set; }

        [Required]
        public bool FullAccess { get; set; }

        [Required]
        public DateTime CreateDate { get; set; }
    }

I want to be able to generate similar code with LLBLGP so I don't have to write it by hand.

I've been able to add the attributes in the Project settings. It works very nicely especially the conditions, however, I have some issues:

  1. Some tables have composite primary keys. Is there an attribute macro that I can use to return the order of the PK field. I need to generate: [Column("PKField1", Order=1)].
  2. I need to add a ForeignKey attribute pointing to the name of the navigator. Is there a way to retrieve that?
[Column("DepartmentId"), ForeignKey("Department")]
public int DepartmentId {get; set;}

public virtual Department {get; set;}

Thanks!

daelmo avatar
daelmo
Support Team
Posts: 8245
Joined: 28-Nov-2005
# Posted on: 10-Aug-2018 10:11:34   

Hi costica,

There is no macros for what you need exactly. It looks more like if a new custom template would resolve your needs. Please elaborate more on what are you trying to achieve here. Why do you need this attributes to be generated?

David Elizondo | LLBLGen Support Team
costica
User
Posts: 2
Joined: 10-Aug-2018
# Posted on: 10-Aug-2018 18:57:45   

There is nothing else I can elaborate on. Basically I inherited the code and there is too much code to start over using a different style. The previous developers coded the EF C# entities classes by hand - for whatever reasons. I guess you are right, I have to change the lpt file that generates the entities and I already did it actually.

If I were to make a suggestion for your product, you might want to extend the available macros - you should have FieldIndex at least for NormalField attributes. Having Navigator names for the FK fields would be a bonus.

Thanks

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39614
Joined: 17-Aug-2003
# Posted on: 10-Aug-2018 22:01:25   

costica wrote:

There is nothing else I can elaborate on. Basically I inherited the code and there is too much code to start over using a different style. The previous developers coded the EF C# entities classes by hand - for whatever reasons. I guess you are right, I have to change the lpt file that generates the entities and I already did it actually.

Not needed I think. The thing is: attributes for mappings or an EDMX file or code first mappings all result in the same thing: the meta-data model created at runtime inside the context. So you can just generate DbContext + EDMX or code first code using the presets for EF6 and all you need to change is how you create your context instance (e.g. if you opt for the edmx variant, you have to specify the edmx file or the assembly it's embedded in). This could save you a lot of time and for your code it doesn't matter: the attributes are only used for mappings.

There are exceptions of course: [Required] attributes and the like are used by controls bound to the entities as well, but you can generate them with rules (you already have them in place) , and leave the mapping specific attributes out of it, so the only attributes you need to generate are the ones used by code bound to the entity instances, IF that's something your code uses. simple_smile

If I were to make a suggestion for your product, you might want to extend the available macros - you should have FieldIndex at least for NormalField attributes. Having Navigator names for the FK fields would be a bonus.

Thanks

Thanks for the suggestion, we'll look into those in a future version simple_smile

Frans Bouma | Lead developer LLBLGen Pro