[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

entity framework extension class and creating a custom class

Posted on 2011-05-12
19
Medium Priority
?
675 Views
Last Modified: 2012-05-11
Im using eneity framework and I would like to create a partail class which i have done.

Namespace CulminITIMSModel



    Partial Public Class Role



        Private _roleID As Integer = 0

        Private _role As String = ""

     

<StringLengthValidator(1, 50, Ruleset:="RuleSetA", _

              MessageTemplate:="First Name must be between 1 and 50 characters")> _


         Public Property Role1() As String

            Get

                Return _role

            End Get

            Set(ByVal value As String)

                _role = value

            End Set

        End Property




  End Class



End Namespace


I want to enter a new record with possibly more than 50 characters for the name, when i run the program the system gives me an autogenerated error message and i want to know why it wont display my custom message.

Please help urgently!
0
Comment
Question by:culminIT
  • 11
  • 8
19 Comments
 
LVL 18

Expert Comment

by:dj_alik
ID: 35745605
you can do this using MetadataType attribute


Adding Metadata to Entities in The Data Model

http://blogs.microsoft.co.il/blogs/gilf/archive/2011/01/20/adding-metadata-to-entities-in-the-data-model.aspx
0
 

Author Comment

by:culminIT
ID: 35745759
We are trying to build a business object on top of an entity framework object. We want our business object to work with the entity framework object, all we want to do in the business object is to do the validation. The question is how do we insert a record by using these two classes to do the validation and to save the record?
0
 

Author Comment

by:culminIT
ID: 35745763
reply to dj_alik - we have tried your suggestion and it was unsuccessful, any more suggestions?
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 18

Expert Comment

by:dj_alik
ID: 35745857
What did you try?
Can i see the code?
0
 

Author Comment

by:culminIT
ID: 35745873
CODE ATTACHED
Namespace CulminITIMSModel

    Public Class BORole

        Private _roleID As Integer = 0
        Private _role As String = "" 

        <StringLengthValidator(1, 50, Ruleset:="RuleSetA", _
             MessageTemplate:="First Name must be between 1 and 50 characters")> _
        Public Property Role1() As String
            Get
                Return _role
            End Get
            Set(ByVal value As String)
                _role = value
            End Set
        End Property

    End Class
    <MetadataType(GetType(BORole))> _
    Partial Public Class Role
    End Class
End Namespace

Open in new window

0
 

Author Comment

by:culminIT
ID: 35745892
dj_alik - the error i received is this:

    Could not load type 'TestLibraryFormsBOL.CulminITIMSModel.BORole' from assembly 'TestLibraryFormsBOL, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null'.
0
 
LVL 18

Expert Comment

by:dj_alik
ID: 35746008
Public Class BORole
 must be partial
0
 

Author Comment

by:culminIT
ID: 35746072
dj-alik making BORole a partial public class was unsuccessful... it gives the same error as before. Do you know what that error means?
0
 
LVL 18

Expert Comment

by:dj_alik
ID: 35746327
you  get exception in debug?
0
 
LVL 18

Expert Comment

by:dj_alik
ID: 35746342
please compare the sample:

Imports System
Imports System.ComponentModel
Imports System.ComponentModel.DataAnnotations

Namespace LPRSDev.DAL
  <MetadataType(GetType(AccountMetaData))>
  Partial Public Class Account

  End Class

  Public Class AccountMetaData
    <StringLength(150, ErrorMessage:="Account Name must be 150 characters or less in length."),Required(ErrorMessage:="Account Name is required.")>
    Public Property AccountName() As System.String

    <StringLength(12, ErrorMessage:="Account Number must be 10 or 12 characters in length.")>
    <Required(ErrorMessage:="Account Number is required.")>
    Public Property AccountNumber() As System.Int64
  End Class
End Namespace

Open in new window

0
 

Author Comment

by:culminIT
ID: 35746531
Our code below is what we have tried and our error messages state:
Namespace TestLibraryFormsDAL

    Public Class BORole


        <StringLength(50, ErrorMessage:="Role Name must be 50 characters or less in length."), Required(ErrorMessage:="Role Name is required.")>
        Public Property Role1() As System.String

        <MetadataType(GetType(BORole))> _
        Partial Public Class Role
        End Class


    End Class

End Namespace

In our form the following code is added to insert a record:
 
Dim myRole = New Role
        With myRole
            .Role1 = "mnjhkyuikolobnvbnbvnvbnvbvbvbwerwerwrwrwrrewerwerwerv"
            context.Roles.AddObject(myRole)
            context.SaveChanges()
       End With
When we use the code above to insert we get the error msg : String or binary data would be truncated.
The statement has been terminated.


When we do the following: 
 Dim myRole = New BORole
        With myRole
            .Role1 = "mnjhkyuikolobnvbnbvnvbnvbvbvbwerwerwrwrwrrewerwerwerv"
            context.Roles.AddObject(myRole)
            context.SaveChanges()
           End With
we get the error: Value of type TestLibraryFormsBOL.TestLibraryFormsDAL.BORole' cannot be converted to 'TestingLibraryFormsDAL.Role

i am so confused, going round in circles.

Open in new window

0
 
LVL 18

Expert Comment

by:dj_alik
ID: 35746618
When we use the code above to insert we get the error msg : String or binary data would be truncated.
The statement has been terminated.
1. Length  of Field Role1 less than from  Role1 = "mnjhkyuikolobnvbnbvnvbnvbvbvbwerwerwrwrwrrewerwerwerv" length
2.check namespaces  between EntitySet and  your class of METADATATYPE



 
0
 
LVL 18

Expert Comment

by:dj_alik
ID: 35746624
1. Length  of Field Role1 in database less than from  Role1 = "mnjhkyuikolobnvbnbvnvbnvbvbvbwerwerwrwrwrrewerwerwerv" length
0
 

Author Comment

by:culminIT
ID: 35746740
why is it giving the automated system error (ie truncated) when we want it to give us the custom message displayed in the code above?
the namespace between the entity set and class of metadatype is correct.
0
 
LVL 18

Expert Comment

by:dj_alik
ID: 35746832
What the ORM you are using?
0
 

Author Comment

by:culminIT
ID: 35746965
We use entity framework model for a SQL database.
0
 

Author Comment

by:culminIT
ID: 35752684
any more ideas dj_alik?
0
 

Accepted Solution

by:
culminIT earned 0 total points
ID: 36984311
Just as an update.

We used POCO classes that auto generate (T4 custom template) with the validaton code (Enterprise Library) which are also partial classes.
We then added additional partial classes to handle the more complicated business logic by using self validation. The self validation then reports on the custom data annotation validations we used on the generated poco objects.

Summary:
3 partial classes
1 : EF4 Entity objects - Generated by Entity Model Generator
2 : POCO Entity - Generated by our custom T4 template
3 : Partial BL objects - Additional business logic using self validation
0
 

Author Closing Comment

by:culminIT
ID: 37010629
Lots of Google and a good measure of trial and error enabled us to solve the problem in house
0

Featured Post

NEW Veeam Backup for Microsoft Office 365 1.5

With Office 365, it’s your data and your responsibility to protect it. NEW Veeam Backup for Microsoft Office 365 eliminates the risk of losing access to your Office 365 data.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
Screencast - Getting to Know the Pipeline

834 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question