Mapping a long data property to int in database

c# entity-framework entity-framework-6

Question

In each and every example I've seen,int32 the IDs are. In working contexts, that isn't always feasible. Identity IDs in several of our databases are in the range ofint64 s, therefore our standard procedure is to employlong the features of our ID. However, the maximum numbers for SQL Server's are higher.int column format.

I'm using Entity Framework version 6 to conduct our first proof of concept. Using thelong ID the object cannot be mapped to the database.

Every single mapping I do makes use of Fluent API. Right now, the ID appears as follows:

Property(s => s.ID).HasColumnName("spcID");

Adding a.HasColumnType("int") I get the following error at the conclusion of the above:

Schema specified is not valid. Errors: (7,12) : error 2019: Member Mapping specified is not valid. The type 'Edm.Int64[Nullable=False,DefaultValue=]' of member 'ID' in type 'EFConnection.Space' is not compatible with 'SqlServer.int[Nullable=False,DefaultValue=,StoreGeneratedPattern=Identity]' of member 'spcID' in type 'CodeFirstDatabaseSchema.Space'.

How do you convert these's datatype into a long variable in.NET?

Edit

I've currently set up a straightforward Integration Test to make sure I can connect:

[TestMethod]
public void TestMethod1() {
    using (var context = new Context()) {
        Assert.IsTrue(context.Spaces.Any());
        Assert.IsTrue(context.Spaces.First().IsActive);
    }
}

Absent the.HasColumnType("int") the initial assertion succeeds, but I receive anInvalidOperationException as for the second:

The 'ID' property on 'Space' could not be set to a 'System.Int32' value. You must set this property to a non-null value of type 'System.Int64'.

1
5
11/29/2016 4:25:04 PM

Accepted Answer

SQL and C# data types work together:

If a column in your table containsbigint use public long Id { get; set; } if your article isint use public int Id { get; set; }

SQL

bigint -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807 int -2,147,483,648 to 2,147,483,647

C#

long -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807 int -2,147,483,648 to 2,147,483,647

References:

11
12/27/2013 4:07:36 PM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow