1

i'm trying to map a legacy database with a legacy POCO model. Since database and model where develop not having in mind Entity Framework there are some little differences between that made me stuck.

The challenge I'm facing is that I would like to make it work being as less invasive as possible (don't want to touch code of the database nor the model) since there is too much codes that depends on.

I have tried to map the entities using a code first approach, reusing the POCOs from the legacy model. Every thing seemed to work find since I found that some nullable numeric columns were mapped to properties that are declared as primitive Int32 (not nullable).

For example, let's say I have a table:

CREATE TABLE [dbo].[SomeTable](
    [Id] [int] NOT NULL,
    [Descrip] [nvarchar](50) NULL,
    [SomeNumericCol] [int] NULL,
 CONSTRAINT [PK_SomeTable] PRIMARY KEY CLUSTERED 
(
    [Id] ASC
)) ON [PRIMARY]

and the corresponding POCO:

public class SomeTable
{
    public Int32 Id { get; set; }
    public string Descrip { get; set; }
    public Int32 SomeNullableCol { get; set; }
}

As you might see there is a difference between the column SomeNullableCol and the corresponding property, since the type of the last is a primitive int which doesn't allow nulls.

Is there a hack to make this mapping work without having to change the type of SomeNullableCol to a nullable int (I mean Int32? ), and if possible not touching the code of the class.

Thanks!

2 Answers 2

2

Yes, just mask it with data annotations. Create a nullable int and mask it to the column name, then create a nonmapped property with that same name that only returns an int value from the nullable column.

        [Column("SomeNullableCol")]
        public int? TestValue { get; set; }

        [NotMapped]
        public int SomeNullableCol
        {
            set { TestValue = value; }
            get
            {
                if (TestValue != null) return (int) TestValue;
                return -1;
            }
        }
Sign up to request clarification or add additional context in comments.

4 Comments

That would solve part of the problem, since I don't want to touch the POCO definition. Nevertheless, it gives me the next idea: inherit from the POCO and add the "proxy" property there. I would try doing that by reflection...
This hack at least maintains the current structure of the POCO so no legacy code will break while enhancing it to your requirements. The other option is to use a middle man. Create a new POCO that matches the legacy DB and then map the old POCO to the new one and not to the DB at all.
Yes. As I said, your answer gaves me the idea to build a type with the nullable property dynamically (using Reflection.Emit). Then I could map the generated type to the database. Thanks.
Property cannot be used in LINQ, so not be translated into sql and exception will occur
1

Can you make the property on your POCO nullable?

public class SomeTable
{
    public Int32 Id { get; set; }
    public string Descrip { get; set; }
    public Int32? SomeNumericCol { get; set; }
}

1 Comment

No, I can't :-( . As I said, I can't change it to a nullable type since there is too much code that relies on those properties to be int. Ant there are a lot of properties too change...

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.