locked
Set type converter for Entity field RRS feed

  • Question

  • Hello.

    Is exists some way to set type converter for Entity if EF?

    e.g. entity contains field (some a complex type) which cann't be mapped to any database type, but I can set for the entity type converter, that will be convert field value to some database type (e.g. to nvarchar or some other).

    Similar future exists in NHibernate.

    Thanks in advance.
    Thursday, August 4, 2011 7:45 PM

Answers

  • Hi Mikhail,

    Thanks for your feedback.

    It doesn't seem to be supported in EF now. Thanks for understanding.

    There is ComplexType in EF, it will store all the type's properties, but you can use [NotMapped] to ignore property. 

    Have a nice day.


    Alan Chen[MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    • Marked as answer by Alan_chen Tuesday, August 23, 2011 5:29 AM
    Monday, August 8, 2011 11:56 AM

All replies

  • Hi Mikhail,

    Welcome!

    To tell you truth, I'm not familiar with NHibernate. In EF4.1 there is Fluent API to configure data type of database column(HasColumnType()) 

    ColumnAttribute 

    Placed on a property to specify the column name, ordinal & data type 

    http://blogs.msdn.com/b/adonet/archive/2011/03/15/ef-4-1-code-first-walkthrough.aspx

    The Enums also supported, you can refer here: 

    http://blogs.msdn.com/b/adonet/archive/2011/06/30/walkthrough-enums-june-ctp.aspx

    Have a nice day.


    Alan Chen[MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Friday, August 5, 2011 6:36 AM
  • Hi Alan,

    thanks for reply.

    it's not exactly what I need.

    E.g. I have entity

    public class SomeEntity {

     public int SomeEntityId {get;set;}

     public string Name {get;set;}

     public EntityFromSomeExternalRepo External {get;set;}

    }

    EntityFromSomeExternalRepo is some entity which can persists in any external source (this entity has ID property)

    when EF try to save SomeEntity in repository i want to point EF how it should save  EntityFromSomeExternalRep (e.g. I whant that EF should save ID of external entity)

    when EF try to load SomeEntity from database, it should apply reverse transformation from external entity ID to EntityFromSomeExternalRepo.

    E.g. in my real project (with Nhibernate and some framework) it's looks like following (User is entity from external repo):

    private static void ConfigureIUsersMapping(FluentDataStoreMapper dataStore)
            {
                var parameterChangeHystoryUserProperty = dataStore.Entity<IParameterChangeHistory>().GetProperty(history => history.UpdateUser);
                parameterChangeHystoryUserProperty.SetTypeConverter(ConvertUserToGuid, ConvertGuidToUser);

                var memoUserProperty = dataStore.Entity<IMemoDetailRejectReason>().GetProperty(reason => reason.User);
                memoUserProperty.SetTypeConverter(ConvertUserToGuid, ConvertGuidToUser);
            }

            private static IUser ConvertGuidToUser<T>(T arg1, Guid arg2)
            {
                if (Guid.Empty == arg2)
                    return null;
                return SecurityManager.Participants.Users[arg2];
            }

            private static Guid ConvertUserToGuid<T>(T arg1, IUser arg2)
            {
                if (arg2 == null)
                    return Guid.Empty;
                return arg2.Id;
            }

     

     

     

    Friday, August 5, 2011 5:41 PM
  • Hi Mikhail,

    Thanks for your feedback.

    It doesn't seem to be supported in EF now. Thanks for understanding.

    There is ComplexType in EF, it will store all the type's properties, but you can use [NotMapped] to ignore property. 

    Have a nice day.


    Alan Chen[MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    • Marked as answer by Alan_chen Tuesday, August 23, 2011 5:29 AM
    Monday, August 8, 2011 11:56 AM
  • Hi,

    I am writing to check the status of the issue on your side. Would you mind letting us know the result of the suggestions?

    If you need further assistance, please feel free to let me know. I will be more than happy to be of assistance. 

    Have a nice day.


    Alan Chen[MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Thursday, August 11, 2011 8:33 AM