Comments (5)
For what it's worth, this does work in EF Core, but you have to update the primary key field in the foreign key table to match the same enum and specify the database column type. So, a working version of my example would be:
public class EntitySimple
{
[Key]
public int ID { get; set; }
public int ColumnInt { get; set; }
[Column("TypeID", TypeName = "int")]
public MyEnumType TypeID { get; set; }
public MyEnumDbType EntityType { get; set; }
}
public enum MyEnumType
{
A = 1,
B = 2,
C = 3
}
public class MyEnumDbType
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.None)]
[Column(TypeName = "int")]
public MyEnumType ID { get; set; }
public string Name { get; set; }
}
The DatabaseGeneratedOption.None
flag tells the model generator to not enable auto-increment on the primary key field, with the assumption that the enum table is manually maintained.
from entityframework-classic.
Hello @tiesont ,
Thank you for reporting, we will look at it.
Best Regards,
Jonathan
from entityframework-classic.
Super. "Yeah, that doesn't work" is a perfectly acceptable response, by-the-way; I just need to know if it's supposed to work, and if so, how to set it up.
Thanks for the quick reply, too.
from entityframework-classic.
Hello @tiesont ,
My developer has looked at it.
He created a Fiddle with your example: https://dotnetfiddle.net/Gnmhda
However, he is not sure about the "My TypeID column is a foreign key, if it's relevant." part.
Do you think you could show him how you are doing it?
from entityframework-classic.
I think that's the issue, then. From that fiddle, my model would look something like
public class EntitySimple
{
public int ID { get; set; }
public int ColumnInt { get; set; }
[Column("TypeID", TypeName = "int")]
public MyEnumType TypeID { get; set; }
public MyEnumDbType EntityType { get; set; }
}
public enum MyEnumType
{
A = 1,
B = 2,
C = 3
}
public class MyEnumDbType
{
public int ID { get; set; }
public string Name { get; set; }
}
The idea is that the values stored in the MyEnumDbType
table are fixed values which are used internally in the application we're building. The table is mostly there for associating a descriptive text value (what we show in the application) with the fixed values of of MyEnumType
; the ID
value of MyEnumDbType
corresponds to one of the MyEnumType
constants.
So, this mostly likely the problem - EF is trying to match the data type, int, of MyEnumDbType.ID
with the EntitySimple.TypeID
value, which it can't do. I'll do some digging, but I think you've shown me what I want to do doesn't work.
from entityframework-classic.
Related Issues (20)
- Type not supported yet (LazyType): sdo_geometry HOT 11
- "Cannot bind argument to parameter 'Path' because it is an empty string" when running Add-Migration HOT 9
- Include with InvalidCastException exception HOT 4
- IDbContext HOT 8
- EF6 is available for .NET Core 3+ HOT 1
- HasNoKey and Constructor with Parameters HOT 5
- Make DbMigrator constructors public HOT 4
- Link to entityframework.net HOT 3
- Count() inside string expression in Execute method HOT 4
- Creating new array inside expression string - Execute extension method HOT 8
- Error when using UpdateFromQuery on an expanded query (Linqkit) HOT 3
- DbMigrator - Problems with existing migrations HOT 6
- HierarchyId.GetAncestor method raise an unexpected exception HOT 7
- Current Values are not reset when using SoftDelete HOT 6
- this.Configuration not availalbe in .net core 6 for soft delete HOT 3
- EDMX - Unable to cast object of type ObjectQuery to DbQuery in .Include() HOT 19
- The object cannot be deleted because it was not found in the ObjectStateManager HOT 1
- IDbCommandInterceptor called multiple times on queries with .IncludeOptimizedByPath() HOT 2
- add new provider Microsoft.Data.SqlClient HOT 3
- Unable to load the specified metadata resource. HOT 5
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from entityframework-classic.