-
Notifications
You must be signed in to change notification settings - Fork 1
DbContext Build
If you wish to create a database or manually model an existing database, that's entirely possible with EFPosh!
To build the DbContext, first you'll need PowerShell classes describing the Sql tables you wish to query / create.
Class MyTable {
[int]$Id
[string]$ColumnOne
}If you are using this to build a database, Entity Framework requires you have a "Primary Key" - this is a non-nullable property that will always be different. Entity Framework requires this because if you wish to edit the data, it needs a way to be able to tell the difference between two rows of data. The primary key can be one or multiple columns, but it needs to be different in every row otherwise you'll get errors accessing your data.
Now that I have my table, I can build out my DbContext! First build a tables array with all the tables you want to add:
$Tables = @(
( New-EFPoshEntityDefinition -Type 'MyTable' -PrimaryKey 'Id' )
)Note: If your primary key is a number (int or long) then Entity Framework will manage it for you. If you create a row and do not specify it, Entity Framework will find the next unused number and use that as the Id auto-magically.
If you do not have a key (for instance, it's a View you are modeling) that is fine, just use the -KeyLess switch when adding the New-EFPoshEntityDefinition. After that, the entity will be read-only.
After creating my Tables array, you then just need to build out the Context!
$Context = New-EFPoshContext -SQLiteFile '.\DbFile.sqlite' -Entities $Tables -EnsureCreatedThe above command will create your database if it is not already created based on the Tables given it! It will not update your database to a new schema (like if you wanted to add a column to your database) - there is currently no way to update the schema with this module.
If you wish to do a MSSql database, just use the -MSSql* parameters instead of the -SqliteFile parameter.