Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. The Lounge
  3. I'm stunned at how stupid EntityFramework is

I'm stunned at how stupid EntityFramework is

Scheduled Pinned Locked Moved The Lounge
databasecomquestion
21 Posts 13 Posters 27 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Marc Clifton

    And how it just does not work with interfaces or base classes. At least, I can't get it to work, so maybe I'm stupid. Doing some googling, there's things like HasBaseType that have to be manually coded in the OnModelCreating method, or in the query, potentially using .Include, but really. Why can't it just figure out what to do based on the class structure? Maybe it's me. But I can understand why people use Dapper. :sigh:

    Latest Articles:
    Client-Side Type-Based Publisher/Subscriber, Exploring Synchronous, "Event-ed", and Worker Thread Subscriptions

    Richard DeemingR Offline
    Richard DeemingR Offline
    Richard Deeming
    wrote on last edited by
    #8

    Using base classes to define common properties for multiple entities just works, in both EF Core and EF6. For example, I often use a TrackedEntity class:

    public abstract class TrackedEntity
    {
    public DateTimeOffset CreatedDate { get; set; }
    public string CreatedBy { get; set; }
    public DateTimeOffset? UpdatedDate { get; set; }
    public string UpdatedBy { get; set; }
    }

    public class Foo : TrackedEntity
    {
    public int Id { get; set; }
    ...
    }

    public class Bar : TrackedEntity
    {
    public Guid Id { get; set; }
    ...
    }

    Each table will have the properties from the base class. If you want more complicated things, there's always Table-per-Type or Table-per-Hierarchy options: Inheritance - EF Core | Microsoft Docs[^]


    "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

    "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

    M 1 Reply Last reply
    0
    • M Marc Clifton

      And how it just does not work with interfaces or base classes. At least, I can't get it to work, so maybe I'm stupid. Doing some googling, there's things like HasBaseType that have to be manually coded in the OnModelCreating method, or in the query, potentially using .Include, but really. Why can't it just figure out what to do based on the class structure? Maybe it's me. But I can understand why people use Dapper. :sigh:

      Latest Articles:
      Client-Side Type-Based Publisher/Subscriber, Exploring Synchronous, "Event-ed", and Worker Thread Subscriptions

      J Offline
      J Offline
      Jorgen Andersson
      wrote on last edited by
      #9

      Are you using Database first or Code first?

      Wrong is evil and must be defeated. - Jeff Ello

      M 1 Reply Last reply
      0
      • M Marc Clifton

        And how it just does not work with interfaces or base classes. At least, I can't get it to work, so maybe I'm stupid. Doing some googling, there's things like HasBaseType that have to be manually coded in the OnModelCreating method, or in the query, potentially using .Include, but really. Why can't it just figure out what to do based on the class structure? Maybe it's me. But I can understand why people use Dapper. :sigh:

        Latest Articles:
        Client-Side Type-Based Publisher/Subscriber, Exploring Synchronous, "Event-ed", and Worker Thread Subscriptions

        pkfoxP Offline
        pkfoxP Offline
        pkfox
        wrote on last edited by
        #10

        I never use EF -I learnt it and decided my own BL/DataAccess model was better and much more flexible. I have a base BL which has its own connection class and I can get a new project CRUDABLE in a very short time. I understand why people use it but it's not for me.

        "I didn't mention the bats - he'd see them soon enough" - Hunter S Thompson - RIP

        1 Reply Last reply
        0
        • M Marc Clifton

          And how it just does not work with interfaces or base classes. At least, I can't get it to work, so maybe I'm stupid. Doing some googling, there's things like HasBaseType that have to be manually coded in the OnModelCreating method, or in the query, potentially using .Include, but really. Why can't it just figure out what to do based on the class structure? Maybe it's me. But I can understand why people use Dapper. :sigh:

          Latest Articles:
          Client-Side Type-Based Publisher/Subscriber, Exploring Synchronous, "Event-ed", and Worker Thread Subscriptions

          E Offline
          E Offline
          Espen Harlinn
          wrote on last edited by
          #11

          Quote:

          But I can understand why people use Dapper

          It will still underperform compared to using the drivers directly - which, as we all know, isn't all that hard.

          Espen Harlinn Senior Architect - Ulriken Consulting AS The competent programmer is fully aware of the strictly limited size of his own skull; therefore he approaches the programming task in full humility, and among other things he avoids clever tricks like the plague.Edsger W.Dijkstra

          1 Reply Last reply
          0
          • K Kenneth Haugland

            Maybe you'll get a mail someday that starts with "I have been maintaining your code for a decade now, and I really HATE you!" :laugh:

            E Offline
            E Offline
            Espen Harlinn
            wrote on last edited by
            #12

            Quote:

            I have been maintaining your code for a decade now, and I really HATE you!

            IMHO: If you work for a decade doing plain CRUD, without automating the process, you should probably look for a different profession …

            Espen Harlinn Senior Architect - Ulriken Consulting AS The competent programmer is fully aware of the strictly limited size of his own skull; therefore he approaches the programming task in full humility, and among other things he avoids clever tricks like the plague.Edsger W.Dijkstra

            1 Reply Last reply
            0
            • Sander RosselS Sander Rossel

              You mean it should create tables based on your inheritance structure? EF can deal with regular inheritance and interfaces just fine, I guess it kind of ignores it. But if you have a class, say Employee, that inherits from Person, a migration won't magically create two tables with Employee having only the Employee specific properties and a reference to Person. I put everything in OnModelCreating (like types, nullable, etc.). Just because you're using an ORM doesn't mean you don't have to think about your DB structure anymore :confused:

              Best, Sander Azure DevOps Succinctly (free eBook) Azure Serverless Succinctly (free eBook) Migrating Apps to the Cloud with Azure arrgh.js - Bringing LINQ to JavaScript

              M Offline
              M Offline
              Marc Clifton
              wrote on last edited by
              #13

              Sander Rossel wrote:

              You mean it should create tables based on your inheritance structure?

              Nah, I don't want EF creating anything. I just thought I'd try using it, but I want control over the inheritance structure and leave it to my responsibility that the fields defined in the base class are implemented in the table that mirrors the derived class. Seem that it doesn't like that.

              Latest Articles:
              Client-Side Type-Based Publisher/Subscriber, Exploring Synchronous, "Event-ed", and Worker Thread Subscriptions

              Sander RosselS 1 Reply Last reply
              0
              • Richard DeemingR Richard Deeming

                Using base classes to define common properties for multiple entities just works, in both EF Core and EF6. For example, I often use a TrackedEntity class:

                public abstract class TrackedEntity
                {
                public DateTimeOffset CreatedDate { get; set; }
                public string CreatedBy { get; set; }
                public DateTimeOffset? UpdatedDate { get; set; }
                public string UpdatedBy { get; set; }
                }

                public class Foo : TrackedEntity
                {
                public int Id { get; set; }
                ...
                }

                public class Bar : TrackedEntity
                {
                public Guid Id { get; set; }
                ...
                }

                Each table will have the properties from the base class. If you want more complicated things, there's always Table-per-Type or Table-per-Hierarchy options: Inheritance - EF Core | Microsoft Docs[^]


                "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

                M Offline
                M Offline
                Marc Clifton
                wrote on last edited by
                #14

                Hmmm, I think when I was testing this, my failure was in declaring the base class as abstract. Like I said, I might be being stupid. :)

                Latest Articles:
                Client-Side Type-Based Publisher/Subscriber, Exploring Synchronous, "Event-ed", and Worker Thread Subscriptions

                D 1 Reply Last reply
                0
                • J Jorgen Andersson

                  Are you using Database first or Code first?

                  Wrong is evil and must be defeated. - Jeff Ello

                  M Offline
                  M Offline
                  Marc Clifton
                  wrote on last edited by
                  #15

                  Database first, but then I learned that .NET Core 3 / EF doesn't support creating the C# model from the DB model unless you use Scaffold-DbContext from the Package Manager console, and it's a bit stunted. It's annoying that the tooling doesn't implement enough flexibility, but that's par for the course.

                  Latest Articles:
                  Client-Side Type-Based Publisher/Subscriber, Exploring Synchronous, "Event-ed", and Worker Thread Subscriptions

                  1 Reply Last reply
                  0
                  • S Super Lloyd

                    Personally I like to keep it simple and straightforward, i.e. 1 table => 1 class. i.e. no hierarchy, interface, etc... Works for me! ^_^ You could argue I am ignoring many powerful features... and you would be right! But it works out quite well for me anyway! ;P

                    A new .NET Serializer All in one Menu-Ribbon Bar Taking over the world since 1371!

                    M Offline
                    M Offline
                    Marc Clifton
                    wrote on last edited by
                    #16

                    I'm trying to implement a certain degree of abstraction, especially for REST calls, where the entities (tables) are passed in as part of the route and I want CRUD methods that are abstracted so I don't have to write unique code for each context entity. My problem is one of laziness - I know how to do this in different ways, thought I'd see how one might do this with EF, but I'm too lazy and impatient to figure out what hoops, canyons and mountains I have to jump through, create a bridge over, or climb with enough oxygen tanks, to get it to work. Maybe I'll google some more. :laugh:

                    Latest Articles:
                    Client-Side Type-Based Publisher/Subscriber, Exploring Synchronous, "Event-ed", and Worker Thread Subscriptions

                    S 1 Reply Last reply
                    0
                    • M Marc Clifton

                      And how it just does not work with interfaces or base classes. At least, I can't get it to work, so maybe I'm stupid. Doing some googling, there's things like HasBaseType that have to be manually coded in the OnModelCreating method, or in the query, potentially using .Include, but really. Why can't it just figure out what to do based on the class structure? Maybe it's me. But I can understand why people use Dapper. :sigh:

                      Latest Articles:
                      Client-Side Type-Based Publisher/Subscriber, Exploring Synchronous, "Event-ed", and Worker Thread Subscriptions

                      J Offline
                      J Offline
                      Jacquers
                      wrote on last edited by
                      #17

                      If you're going to use Dapper have a look at [SqlKata the dotnet SQL query builder](https://sqlkata.com/) as well :)

                      1 Reply Last reply
                      0
                      • M Marc Clifton

                        Sander Rossel wrote:

                        You mean it should create tables based on your inheritance structure?

                        Nah, I don't want EF creating anything. I just thought I'd try using it, but I want control over the inheritance structure and leave it to my responsibility that the fields defined in the base class are implemented in the table that mirrors the derived class. Seem that it doesn't like that.

                        Latest Articles:
                        Client-Side Type-Based Publisher/Subscriber, Exploring Synchronous, "Event-ed", and Worker Thread Subscriptions

                        Sander RosselS Offline
                        Sander RosselS Offline
                        Sander Rossel
                        wrote on last edited by
                        #18

                        I've never had an issue with that... However, I use my entities as just that, entities. "Dumb" classes that do nothing except hold some data. Whenever I want to do something, anything, with that data, I create a model class. I've worked with entities before and it always comes back to bite you (lazy loading, then NOT lazy loading, circular references, etc.).

                        Best, Sander Azure DevOps Succinctly (free eBook) Azure Serverless Succinctly (free eBook) Migrating Apps to the Cloud with Azure arrgh.js - Bringing LINQ to JavaScript

                        1 Reply Last reply
                        0
                        • M Marc Clifton

                          Hmmm, I think when I was testing this, my failure was in declaring the base class as abstract. Like I said, I might be being stupid. :)

                          Latest Articles:
                          Client-Side Type-Based Publisher/Subscriber, Exploring Synchronous, "Event-ed", and Worker Thread Subscriptions

                          D Offline
                          D Offline
                          Dan Neely
                          wrote on last edited by
                          #19

                          Good to know. At first I was worried it might have been a feature lost during the move from EF6 to EFcore. I'd seen the partial class trick used to apply interfaces/base classes once before; naturally it was about a year after I'd worked on a different project where knowing the feature was available would have let me take a copy/paste code reduction from ~90% to ~98% fewer lines for the segment I was completely rewriting.

                          Did you ever see history portrayed as an old man with a wise brow and pulseless heart, weighing all things in the balance of reason? Is not rather the genius of history like an eternal, imploring maiden, full of fire, with a burning heart and flaming soul, humanly warm and humanly beautiful? --Zachris Topelius Training a telescope on one’s own belly button will only reveal lint. You like that? You go right on staring at it. I prefer looking at galaxies. -- Sarah Hoyt

                          1 Reply Last reply
                          0
                          • M Marc Clifton

                            I'm trying to implement a certain degree of abstraction, especially for REST calls, where the entities (tables) are passed in as part of the route and I want CRUD methods that are abstracted so I don't have to write unique code for each context entity. My problem is one of laziness - I know how to do this in different ways, thought I'd see how one might do this with EF, but I'm too lazy and impatient to figure out what hoops, canyons and mountains I have to jump through, create a bridge over, or climb with enough oxygen tanks, to get it to work. Maybe I'll google some more. :laugh:

                            Latest Articles:
                            Client-Side Type-Based Publisher/Subscriber, Exploring Synchronous, "Event-ed", and Worker Thread Subscriptions

                            S Offline
                            S Offline
                            Super Lloyd
                            wrote on last edited by
                            #20

                            I never used oData.. but it looks like LinQ 2 SQL code over WebAPI, could it be something that would be useful to you? And I remembered being excited about it... but then, in some context it's doomed to obsolescence because exposing the DB is often a no-no.. But it's a god send me think for SPA or private phone apps... Anyway, if you are curious, here is a link: [OData documentation - OData | Microsoft Docs](https://docs.microsoft.com/en-us/odata/)

                            A new .NET Serializer All in one Menu-Ribbon Bar Taking over the world since 1371!

                            1 Reply Last reply
                            0
                            • S Super Lloyd

                              Personally I like to keep it simple and straightforward, i.e. 1 table => 1 class. i.e. no hierarchy, interface, etc... Works for me! ^_^ You could argue I am ignoring many powerful features... and you would be right! But it works out quite well for me anyway! ;P

                              A new .NET Serializer All in one Menu-Ribbon Bar Taking over the world since 1371!

                              O Offline
                              O Offline
                              obermd
                              wrote on last edited by
                              #21

                              Agreed. Frameworks are great if you're solving the same problem as the framework developers. If you're trying to solve something else they're a waste of time.

                              1 Reply Last reply
                              0
                              Reply
                              • Reply as topic
                              Log in to reply
                              • Oldest to Newest
                              • Newest to Oldest
                              • Most Votes


                              • Login

                              • Don't have an account? Register

                              • Login or register to search.
                              • First post
                                Last post
                              0
                              • Categories
                              • Recent
                              • Tags
                              • Popular
                              • World
                              • Users
                              • Groups