• 0

Generic Constraint for a Record Type and MySQL EF Question


Question

First question,

Not sure if I can constrain this better,

let MapRecord<'t when 't : comparison and 't : equality and 't : not struct> =
/*OMITTED*/
this calls PreComputeRecordConstructor to create a record based on data ...

Second question,

AFAIK F# has no type provider that supports MySQL - only MS SQL.

Thus, if I want to do parsing on MySQL data the best solution is to,

1. Create a C# library with the generated EDMX file (database first)

2. Reference said library in F# code in use it

How sane does this sound? Is there a better solution?

Also, on the same topic as question 2,

let inline getId item = (^a : (member ID : int) item)
don't work in EF queries. EF actually sees the getId funct and does not recognize it throwing an error.

I thought that such code gets resolved and inlined into a direct .memberName call when compiled in IL?

4 answers to this question

Recommended Posts

  • 0

The SQLProvider supports MySQL, according to the project home page.

 

The way inline resolves static generic constraints is by - as the name implies - inlining: it won't work if you assign the function to a non-inline one.

  • 0
  On 15/07/2015 at 16:01, Andre S. said:

The SQLProvider supports MySQL, according to the project home page.

 

The way inline resolves static generic constraints is by - as the name implies - inlining: it won't work if you assign the function to a non-inline one.

Hmm. I am hesitant on trying that out as it is a 3rd party solution.

EDIT: I mean, would you use it or rely on a mutli-project multi-language setup instead?

I worked around by adding an interface to the generated classes and using 'a when 'a :> IFooBar - granted more code.

But I still don't get it, such code works just fine after the query is returned but

when using to filter output from a DbSet it fails at runtime with an EF error complaining that it doesn't support it.

Is it just not possible to use inlined generic functions with EF's _ DbSet query?

This works because queryDb returns the list result of an executed query and then inlined func is applied,

    let inline private getCode item = (^a : (member Code : _) item)
    let inline private getType item = (^a : (member Type : _) item)
    let inline private getDescription item = (^a : (member Description : _) item)

    let inline private getCodeInfo item = getCode item, getDescription item
    let inline private getTypeInfo item = getType item, getDescription item

    let inline private xInfo (table : FooBarEntities -> _ DbSet) f = 
        queryDb (fun db -> table db) |> List.map f |> Map.ofList

    let inline private codeInfo table = xInfo table getCodeInfo
    let inline private typeInfo table = xInfo table getTypeInfo
EDIT:

Actually I can't see to do something like,

        query {
            for item in set do
            where (filter item) //(filter item)
            select item
        }
at all. Seems this is completely different than C# .Where(filter) function.

I am not sure how to approach this. I will try to use C#' Where with an Expression in F# instead.

EDIT: ###### that I will write these extensions methods in C# - can't figure out the heads and taiils of why EF complains about Invoke

Edited by _Alexander
  • 0
  Quote
Hmm. I am hesitant on trying that out as it is a 3rd party solution.

EDIT: I mean, would you use it or rely on a mutli-project multi-language setup instead?

My guess is that working with a type provider should be less painful than EF. But there are several options, perhaps the documentation here can answer some of your woes with LINQ etc.:

http://fsharp.org/guides/data-access/#sql-data-access

 

To be honest, I know very little about EF, SQL and type providers, so unless there are F# afficionados other than me here, I would suggest you ask the question on Stackoverflow. F#'s community is very supportive, but not well represented here.

  • 0

If anyone is wondering on a record constraint, I got this by looking at the metadata from a C# project,

public sealed class TestRecord : IEquatable<TestRecord>, IStructuralEquatable, IComparable<TestRecord>, IComparable, IStructuralComparable
so F# constraint would look like,

    let RecordMap<'record 
        when 'record :> IEquatable<'record> 
        and 'record :> IStructuralEquatable 
        and 'record :> IComparable<'record>
        and 'record :> IComparable
        and 'record :> IStructuralComparable
        and 'record : comparison 
        and 'record : equality 
        and 'record : not struct> =
I think I figured out the EF.

EF uses reflection and Expressions to determine what query to build - the way I was doing it was NOT an F# specific issue - same issue happens in C#.

I hope Microsoft will expand F# to support ASP.NET, WPF, and refactoring in the future. Good language - horrid support.

This topic is now closed to further replies.
  • Posts

    • Is NAD a legitimate court? Nope, it's part of the BBB. So they can allege whatever they want. Guilt is the result of being convicted by an actual recognized legitimate court. Just sayin.
    • If it's right for someone's use case, why not? For the eye watering amount of $$$ involved in acquiring 32GB of SSDs I would want more real RAID.
    • Last chance: SQL Essentials For Dummies eBook (worth $10) free download by Steven Parker FOR DUMMIES is a trademark of John Wiley & Sons, Inc. A right-to-the-point guide on all the key topics of SQL programming SQL Essentials For Dummies is your quick reference to all the core concepts of SQL—a valuable common standard language used in relational databases. This useful guide is straightforward—with no excess review, wordy explanations, or fluff—so you get what you need, fast. Great for a brush-up on the basics or as an everyday desk reference, this book is one you can rely on. Strengthen your understanding of the basics of SQL Review what you've already learned or pick up key skills Use SQL to create, manipulate, and control relational databases Jog your memory on the essentials as you work and get clear answers to your questions Perfect for supplementing classroom learning, reviewing for a certification, and staying knowledgeable on the job, SQL Essentials For Dummies is the convenient, direct, and digestible reference you've been looking for. Claim your complimentary eBook worth $10 for free, before the offer ends today, June 17. How to get it Please ensure you read the terms and conditions to claim this offer. Complete and verifiable information is required in order to receive this free offer. If you have previously made use of these free offers, you will not need to re-register. While supplies last! Download the SQL Essentials For Dummies eBook (worth $10) for free The below offers are also available for free for a limited time: Unruly: Fighting Back when Politics, AI, and Law Upend [...] ($18 Value) FREE - Expires today 6/17 SQL Essentials For Dummies ($10 Value) FREE – Expires today 6/17 Continuous Testing, Quality, Security, and Feedback ($27.99 Value) FREE – Expires 6/18 VideoProc Converter AI v7.5 for FREE (worth $78.90) – Expires 6/18 Macxvideo AI ($39.95 Value) Free for a Limited Time – Expires 6/22 Microsoft 365 Copilot At Work ($60 Value) FREE – Expires 6/25 Natural Language Processing with Python ($39.99 Value) FREE – Expires 6/25 Excel Quick and Easy ($12 Value) FREE – Expires 6/24 The Inclusion Equation: Leveraging Data & AI ($21 Value) FREE – Expires 6/24 The Ultimate Linux Newbie Guide – Featured Free content Python Notes for Professionals – Featured Free content Learn Linux in 5 Days – Featured Free content Quick Reference Guide for Cybersecurity – Featured Free content We post these because we earn commission on each lead so as not to rely solely on advertising, which many of our readers block. It all helps toward paying staff reporters, servers and hosting costs. Other ways to support Neowin The above deal not doing it for you, but still want to help? Check out the links below. Whitelist Neowin by not blocking our ads Create a free member account to see fewer ads Make a donation to support our day to day running costs Subscribe to Neowin - for $14 a year, or $28 a year for an ad-free experience Disclosure: An account at Neowin Deals is required to participate in any deals powered by our affiliate, StackCommerce. For a full description of StackCommerce's privacy guidelines, go here. Neowin benefits from shared revenue of each sale made through the branded deals site.
    • Movavi Video Editor 25.9.0 by Razvan Serea With Movavi Video Editor, you can either enhance your video files with two or three simple steps, or turn them into something completely new. Create your own movies using multiple filters, transitions, and special effects: show multiple videos on one screen with the Picture in picture effect or change the background with the Chroma Key effect, imitate the camera zoom or make your video look like an old-style movie. Adjust video parameters such as brightness, contrast and colors. Stabilize shaky footage, improve video quality and remove defects. Create video presentations, tutorials or educational videos: add titles and record your own narration to create a video with voiceover. Import video from any source: TV-tuner, webcam, camcorder, or VHS. Drop multiple media files onto a timeline and let your imagination do the rest! Features at a glance: Video and audio editing on a timeline Edit, enhance videos Add background music Apply titles and effects Image quality improvement Hollywood-worthy effects High-grade titles and fades Digitize VHS tapes, record video from TV tuners Stabilize any shaky sections Support for a wide range of formats Prepare your videos for uploading to YouTube, Facebook, Vimeo, or any other website New in Movavi Video Editor 2025: Revamped timeline for easier editing The new timeline is now clearer and more streamlined. Get your projects done faster and have more fun with anything – from short vids for socials to longer family movies. Frame-precise cuts in a click Give your videos a sharper look with the new Blade tool. Easily make precise cuts and create eye-catching montages like your favorite bloggers. Pro-quality color correction Get next-level color correction with the same simplicity. Boost colors in a snap and make more viewers fall in love with your videos. AI motion tracking Enhance reality in your videos with additional moving graphics. Just click, and AI will quickly attach any photos, videos, emojis, or memes to objects in your footage. Perfect-match overlay effects Now each overlay effect has 13 blending modes to choose from. Try each of them with the press of a button and pick the one that fits your video perfectly. Best video effects – at your fingertips Create awesome videos in any style with our huge collection of professionally designed effects. Now you can try them all right away, right in the app. Movavi Video Editor 25.9.0 changelog: New Copy effects from one clip to another in a snap. Just right-click the clip → Clip effects → Copy effects. Then right-click again to paste. Try it once, and it might just become your new favorite feature. Improvements Enjoy speedier, smoother previews – now up to 20% faster! Hover over any clip to see the difference. Especially handy when you’ve got a load of clips on the timeline. Speed up editing with new keyboard shortcuts. Ctrl + Z to undo, Ctrl + scroll to zoom the timeline. You’ll find the full list in the Help menu. Fixed issues Some under-the-hood improvements that’ll be useful for faster content creation. Download: Movavi Video Editor Plus 25.9.0 | 5.1 MB (Shareware) View: Movavi Video Editor Plus Website | Screenshot Get alerted to all of our Software updates on Twitter at @NeowinSoftware
  • Recent Achievements

    • Week One Done
      Rhydderch earned a badge
      Week One Done
    • Experienced
      dismuter went up a rank
      Experienced
    • One Month Later
      mevinyavin earned a badge
      One Month Later
    • Week One Done
      rozermack875 earned a badge
      Week One Done
    • Week One Done
      oneworldtechnologies earned a badge
      Week One Done
  • Popular Contributors

    1. 1
      +primortal
      699
    2. 2
      ATLien_0
      272
    3. 3
      Michael Scrip
      214
    4. 4
      +FloatingFatMan
      186
    5. 5
      Steven P.
      144
  • Tell a friend

    Love Neowin? Tell a friend!