Using EntityFramework with datatables editor

Using EntityFramework with datatables editor

adam.leinen@fblfinancial.comadam.leinen@fblfinancial.com Posts: 4Questions: 2Answers: 0

We are starting to use editor to access our database however we currently use EF and our existing models variable names do not match the database names. Can Editor use the EF data annotations to get the column names instead of using the variable names? If it can't are you looking at doing this for a future release? We are trying to avoid renaming all of our variables.

Answers

  • allanallan Posts: 63,494Questions: 1Answers: 10,470 Site admin

    Hi Adam,

    Thanks for your question. At this time, I'm afraid to say that no, we aren't looking to do a complete EF integration for Editor. I did look at it originally, but decided that the ADO.NET data connection was going to be more flexible rather than being bound to just EF.

    That said, are you able to use reflection or something other method to read the data annotations (probably of your EF models)? If so, that it would be entirely possible to set up an Editor instance based on that. Are you able to show me one of your EF models please?

    Thanks,
    Allan

  • adam.leinen@fblfinancial.comadam.leinen@fblfinancial.com Posts: 4Questions: 2Answers: 0

    Here is a sample of one of our models. As you can see the Column annotation is our column name which is currently different than the variable name (Ex: commentType). I am able to read the annotations but how would I pass that into Editor?

        [Serializable()]
        [DataContract(Name = "comment")]
        [Table("FMIT_COMMENT")]
        public class CommentType
        {
            public CommentType()
            {
    
            }
    
            [Key]
            [Column("I_COMMENT", TypeName = "int")]
            [Display(Name = "Comment ID")]
            [DataMember(Order = 10, Name = "id")]
            public int id { get; set; }
    
            [Column("N_COMMENT", TypeName = "varchar")]
            [StringLength(50)]
            [Required]
            [Display(Name = "Comment Type")]
            [DataMember(Order = 20, Name = "commentType")]
            public String commentType { get; set; }
    }
    
  • adam.leinen@fblfinancial.comadam.leinen@fblfinancial.com Posts: 4Questions: 2Answers: 0

    I read right over the section in your getting started page about Procedural style. I was able to use reflection to read the data annotation from my EF model. Let me know if you have any suggestions how to make it better. Also just a note I used Editor 1.9.6 from nuget and had what seems to be the same issue you've had with nuget in the past. I swapped it out with the DLL from the trial and it got past the error.

                TableAttribute tableName = (TableAttribute)obj.GetType().GetCustomAttribute(typeof(TableAttribute));
    
                Editor editor = new Editor(db, tableName.Name, keyColumn);
    
                #region Loop through the properties in the object and add them to the editor object
                foreach (PropertyInfo property in properties)
                {
                    #region Process Entity Framework Data Annotations
                    ColumnAttribute column = (ColumnAttribute)property.GetCustomAttribute(typeof(ColumnAttribute));
                    Field field = new Field(column.Name, property.Name);
    
                    if ((KeyAttribute)property.GetCustomAttribute(typeof(KeyAttribute)) != null)
                    {
                        field.Set(false);
                    }
    
                    StringLengthAttribute columnLength = (StringLengthAttribute)property.GetCustomAttribute(typeof(StringLengthAttribute));
                    if (columnLength != null)
                    {
                        if (!String.IsNullOrEmpty(columnLength.ErrorMessage))
                        {
                            field.Validator(Validation.MaxLen(columnLength.MaximumLength, new ValidationOpts
                            {
                                Message = columnLength.ErrorMessage
                            }));
    
                        }
                        else
                        {
                            field.Validator(Validation.MaxLen(columnLength.MaximumLength));
                        }
                    }
    
                    if ((RequiredAttribute)property.GetCustomAttribute(typeof(RequiredAttribute)) != null)
                    {
    
                        DisplayAttribute displayAttribute = (DisplayAttribute)property.GetCustomAttribute(typeof(DisplayAttribute));
    
                        if (displayAttribute != null)
                        {
                            String msg = displayAttribute.Name + " is Required";
                            field.Validator(Validation.NotEmpty(new ValidationOpts
                            {
                                Message = msg
                            }));
                        }
                        else
                        {
                            field.Validator(Validation.NotEmpty());
                        }
                    }
    
                  
                    #endregion
    
    
                    // Add Field
                    editor.Field(field);
    
                }
    
    
  • allanallan Posts: 63,494Questions: 1Answers: 10,470 Site admin

    Hi Adam,

    That looks superb - many thanks for sharing that solution! I think it looks great. The only limitation is that you need another attribute for each validation type, but that might be acceptable, and if we build something like this into the library it would be something we deal with anyway.

    Also just a note I used Editor 1.9.6 from nuget and had what seems to be the same issue you've had with nuget in the past.

    Darn - thanks for letting me know about that. I thought I'd finally got it fixed!

    Regards,
    Allan

  • ezdavisezdavis Posts: 35Questions: 3Answers: 0

    I would also appreciate a full entity framework example. I'm making it work but a full integration would be nice.

Sign In or Register to comment.