Giter VIP home page Giter VIP logo

Comments (5)

tiesont avatar tiesont commented on June 12, 2024 1

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.

JonathanMagnan avatar JonathanMagnan commented on June 12, 2024

Hello @tiesont ,

Thank you for reporting, we will look at it.

Best Regards,

Jonathan

from entityframework-classic.

tiesont avatar tiesont commented on June 12, 2024

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.

JonathanMagnan avatar JonathanMagnan commented on June 12, 2024

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.

tiesont avatar tiesont commented on June 12, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.