Note that there are some explanatory texts on larger screens.

plurals
  1. POC#: Change Retrieval DataType for Entity Framework
    primarykey
    data
    text
    <p>I'm trying to convert a project that is currently using a custom DAO framework to using Entity Framework. The system is quite large so changes to the database (a SQL Azure DB if that matters) itself aren't particularly feasible and should be avoided if possible.</p> <p>The problem is with the ID column. Unfortunately when the system was created, there are some tables that have a <code>bigint</code> datatype, and some that have an <code>int</code> - but the models themselves all come from a base class having a <code>long</code> for the ID. The previous framework was able to handle this situation, but I have been unable to find a way to do it with entity framework.</p> <p>Below is the most trivial example I can think of:</p> <pre><code>public class Context : DbContext { public IDbSet&lt;Foo&gt; Foos {get;set;} public IDbSet&lt;Bar&gt; Bars {get;set;} } public abstract class BaseClass { public long ID; } public class Foo : BaseClass { ... } public class Bar : BaseClass { ... } SQL Table: Foo +-------------+ | id : bigint | | ... | +-------------+ SQL Table : Bar +-------------+ | id : int | | ... | +-------------+ </code></pre> <p>When I try to load a <code>Bar</code> model, I get this error:</p> <pre><code>The 'ID' property on 'BaseClass' could not be set to a 'Int32' value. You must set this property to a non-null value of type 'Int64'. </code></pre> <p>I'd like to find a way to tell the system that Bar has ints, while Foo has longs. I've tried overriding <code>OnModelCreating</code> in the context and defining <code>HasColumnType</code> for <code>Bar</code>. That gave me a new error:</p> <pre><code>Schema specified is not valid. Errors: (105,12) : error 2019: Member Mapping specified is not valid. The type 'Edm.Int64[Nullable=False,DefaultValue=]' of member 'ID' in type 'Bar' is not compatible with 'SqlServer.int[Nullable=False,DefaultValue=,StoreGeneratedPattern=Identity]' of member 'ID' in type 'CodeFirstDatabaseSchema.Bar'. </code></pre> <p>It seems to me that if I could only change the expected data type for the <code>ID</code> of <code>BaseClass</code> to <code>int</code> before sending the request to the server, then I should be able to up-convert to a <code>long</code> after I receive the response. Ideally, I'd like to do this on a per-class basis.</p> <p>Can anyone point me in the right direction?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload