This was a failure. I'm making a note here: huge regret. It's hard to overstate my dissatisfaction. π°
So, I started to build this thinking "man, it really sucks that you can't create an EntityFramework client from a Prisma schema." Except I'm frequently incorrect on things, and oh by the way yes, yes you totally can create an EntityFramework client from Prisma, and without any code in-between. Here's an article on using prisma migrate to deploy your schema, and using the dotnet cli's entityframework introspection features to much more natively produce a client..
Shout out to @YassinEldeeb for building the awesome bootstrap project create-prisma-generator. Without it, this would have taken significantly more time to get into a deployable state.
Prisma. It's great, but if you're from the .NET clan, you're left standing out in the rain. Maybe there's still a way? What if the prisma.schema
file could generate an EntityFramework client?
In your schema.prisma
file, add a new generator called entityframework
(or whatever you like):
// + generator entityframework {
// + provider = "npx prisma-generator-entityframework"
// + output = "../types"
// + namespace = "MyNamespace"
// + clientClassName = "DataDbContext"
// + }
datasource db {
provider = "postgresql"
url = "postgresql://user:password@my_postgres_host.com:5432/initial_db"
}
// Here's some example model code
model User {
id Int @id @default(autoincrement())
email String @unique
name String?
posts Post[]
comment Comment[]
@@map("system_user")
}
...
Run prisma generate
or npx prisma generate
on your schema.
The prisma-generator-entityframework
declaration you added will generate a C# EntityFramework
client interface based on the models you have declared in your schema file.
In your C# project(s), you now should be able to do things like:
using MyNamespace;
...
var context = new DataDbContext(); // if you're new to EntityFramework, it will come preconfigured with smarts to call your db connection, no batteries required!
context.User.Add(new User {
email = "john.doe@gmail.com",
name = "John Doe",
});
context.SaveChanges();
Console.WriteLine(context.User.Where(user => user.name == "John Doe").First().email);
// john.doe@gmail.com
Configuration is as simple as providing values for these four properties:
Property | Type | Description |
---|---|---|
provider |
"npx prisma-generator-entityframework" |
Tell prisma you want to use this generator. |
output |
string : relative or absolute path |
Tell prisma where you want the source code to be dumped to. |
namespace |
string |
Tell prisma-generator-entityframework what namespace to stick your client and model code in. |
clientClassName |
string |
Tell prisma-generator-entityframework what to name your DbContext subclass. |
Platform | Version | Support |
---|---|---|
.NET core | 5.0+ | βοΈ |
.NET core | <5.0 | β (unverified) |
.NET framework | * | β |
Right now, the primary target is .NET core, version 5.0 and later. If enough any interest is communicated in suppporting .NET framework, it can certainly be prioritized.
Prisma connector | Supported | .NET core provider mapping |
---|---|---|
postgres | βοΈ | Npgsql.EntityFrameworkCore.PostgreSQL |
mysql | βοΈ | Pomelo.EntityFrameworkCore.MySql |
sqlite | βοΈ | Microsoft.EntityFrameworkCore.Sqlite |
sqlserver | βοΈ | Microsoft.EntityFrameworkCore.SqlServer |
cockroachdb | β | -* |
mongodb | β | - |
* It seems at least plausible to support CockroachDB, and given how compelling a product the CockroachLabs team have created, this should probably prioritized.
For more information on EntityFramework database provider support, visit the DbContext configuration guide.
For more information on Prisma-supported database connectors, visit the Prisma database connector documentation.
The following table tracks feature availability. It's a good reference for verifying whether your schema will output with the information you need. Drop an issue if you'd like to see a specific feature prioritizied.
Feature | Supported | Description |
---|---|---|
model generation | βοΈ | The system can generate basic models. |
client generation | βοΈ | The system can generate a basic client (DbContext in the EntityFramework world). |
.env datasource | βοΈ | The system can optionally configure the client from a .env file using the env() expression |
relation generation | βοΈ | The system can generate the code necessary to have object-to-object relations. |
table/field mapping | βοΈ | The system can detect @map and @@map annotations, and apply them accordingly. |
array-type field mapping | βοΈ | The system can detect whether a particular field is an array type. |
@id mapping |
βοΈ | The system can map a primary key. |
multi-field @id mapping |
βοΈ | The system can handle multi-field primary keys. |
@default(uuid()) annotation mapping |
βοΈ | The system can specify a limited set of default values for primary key types: integer && string uuid . |
@db.UniqueIdentifier , @db.Uuid |
βοΈ | The system can handle system-specific UUID (aka GUID) types. |
@db* annotation mapping (postgres) |
βοΈ | The system can tell EntityFramework that your postgres @db annotations correspond to important underlying type mappings. |
Basic Json type mapping |
βοΈ | The system can retrieve Json as a string type.* |
Bytes type mapping |
βοΈ | The system can handle the Bytes type as a byte[] |
Unsupported type mapping |
β | " " " |
@default annotation mapping |
β | The system cannot yet apply the full range of model annotations based on the @default field annotation. |
@db* annotation mapping |
β | The system cannot yet apply the full range of model annotations based on the @db.* and @dbgenerated field annotations, beyond postgres. |
property/class case formating | β | The system cannot yet massage case conventions, ie camelCase to PascalCase . |
@index annotation mapping |
β | " " " |
@ignore annotation mapping |
β | " " " |
cuid/autoincrement/now |
β | " " ". Note that uuid is implemented for primary keys. |
nuget dependency detection | β | The system cannot yet autodetect that a nuget dependency is necessary to support the declared db provider. |
enums generation | β | The system cannot yet derive enum s. |
schema model argument mapping | β | The system cannot yet handle model argument mapping. |
* In the future, support may be added for extracting structured types out of json
& jsonb
fields.