Entity Framework Core

main
Yik Teng Hie 3 years ago
parent 52e3c04357
commit e5d212c146

@ -0,0 +1,47 @@
# Guide to modern C++
- Essentials of Modern C++ Style [CppCon 2014](https://www.youtube.com/watch?v=xnqTKD8uD64&t=2304s)
- [Style Guides](https://lefticus.gitbooks.io/cpp-best-practices/content/03-Style.html)
- [Herb Stutter's style](https://herbsutter.com/elements-of-modern-c-style/)
- [Bjarne Stroustrup's C++ Style](https://www.stroustrup.com/bs_faq2.html)
## C++20
- Error :
- C2664 - cannot convert ... 'const[4] to char*'
- C2132 expression didnot evalute to a constant
```C++
void myFn(char* data)
{
std::cout << data;
return;
}
int main()
{
myFn("ABC"); ==> error
myFn(const_cast<char*>("ABC")); ==> OK
std::cout << "Hello World!\n";
}
constexpr struct stIniEnumToFilenameMap LogNameMap[] =
{
{CFileLocation::DEBUG_LOG, const_cast<TCHAR*>("Dbg.log")},
{CFileLocation::DEBUG_TIMING_LOG, const_cast<TCHAR*>("DbgTiming.log"},
{CFileLocation::GUIEVENT_LOG, const_cast<TCHAR*>("GUIEventLog.log"},
};
```
- range for loop `for each()`
```C++
for each (auto x in items){} ==> error
for (auto x : items) {} ==> OK
```
- Error: C2440 'initializing': cannot convert from 'ATL::CA2CA' to 'std::basic_string<char,std::char_traits<char>,std::allocator<char>>'
```C++
std::string strModelName = hardwareIniMotionCard.GetString(osKey.str().c_str(), "Error", FALSE).GetString(); ==> OK - use CString().GetString()
```

@ -0,0 +1,4 @@
# Visual Studio Information
- [Troubleshooting](TroubleshootingGuide.md)
- []

@ -0,0 +1,19 @@
# Issues
## heap debug error
- Symptom : Calling DLL function with std::string parameter / return value, causing heap debug error upon return
- Cause: The caller DLL must compile using the same `[Code Generation]` - `[Runtime Library]` flag. eg: `/MDd` Multi-threaded Debug DLL
- Remedy: Update the flag accordingly
## Compile erro on Simple console project consuming MFC related DLL
- Symptom : Compile Error C1189: #error : Building MFC application with /MD[d] (CRT dll version) requires MFC shared dll version. Please #define _AFXDLL or do not use /MD[d]
- Cause : Default console project for `Advance`->`Use of MFC` is `Use Standard Windows Libraries`
- Remedy: Switch setting to `Use MFC in a Shared DLL`
## Compile error for string parameters
- Symptom: Unable to match LPCSTR parameters / arguments
- Cause: `Advanced`->`Character Set` Setting mismatch. Default VS2022 project is using `Unicode Character set`.
- Remedy: Change to `Multi-byte Character Set` if original DLL is using MBCS Character Set
## Database open fail on 64-bit (C#)
- Symptom:

@ -0,0 +1,75 @@
# Entity Framework Core
- As of 2022, mdb entity ONLY support EF Core 3.x
- [EntityFrameworkCore.Jet](https://github.com/bubibubi/EntityFrameworkCore.Jet)
- [Tutorial](https://www.entityframeworktutorial.net/efcore/entity-framework-core.aspx)
- [EF Core best practice (Youtube)](https://www.youtube.com/watch?v=qkJ9keBmQWo&t=7122s)
## Code First Approach
- Create the code for DbContext and models
- Run the migration script to generate the db tables
- [Tutorial](https://www.yogihosting.com/code-first-entity-framework-core/)
```PM
PM> add-migration InitialDb
PM> update-database
PM> remove-migration
PM> Drop-Database
PM> script-migration
```
## Database First Approach (Reverse Engineering)
- Note: For mdb, this only work with Jet.OleDB Provider
- Generate DbContext & model from database
- [Tutorial] (https://github-wiki-see.page/m/bubibubi/EntityFrameworkCore.Jet/wiki/Using-Entity-Framework-Core-with-Jet-in-Traditional-.Net-Applications)
- Nuget package
- Microsoft.EntityFrameworkCore (3.1.31)
- EntityFrameworkCore.Jet.OleDb (3.1.1)
- Microsoft.EntityFrameworkCore.Tools (3.1.31)
```PM
PM> Scaffold-DbContext "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\myFolder\MitLanguage.mdb;" EntityFrameworkCore.Jet -OutputDir Models -DataAnnotations -Force
```
## Connection String
- [ConnectionString](https://www.connectionstrings.com/)
- Using OleDb (EntityFrameworkCore.Jet.OleDb)
- x64 : `Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\\myFolder\\MitLanguage.mdb;`
- x86 : `Provider=Microsoft.Jet.OLEDB.4.0;Data Source=D:\\Machine\\Database\\MitMc.mdb;`
- Using ODBC (EntityFrameworkCore.Jet.Odbc)
- x64 : `Driver={Microsoft Access Driver (*.mdb, *.accdb)}; Dbq = C:\\myFolder\\MitCustom.mdb; Exclusive=1; Uid = Admin; Pwd =;`
- x86 : `Driver={Microsoft Access Driver (*.mdb)}; Dbq = C:\\myFolder\\MitCustom.mdb; Exclusive=1; Uid = Admin; Pwd =;`
- For x86, use `ODBC Data Source (32-bit)` to add the UserDSN driver for 32-bit platform
## Sample Query
```C#
using EFCoreTest.Models;
using System.Text.Json;
using (var db = new CustomContext())
{
ReelAutoChanger? reelAutoChanger = db.ReelAutoChanger.FirstOrDefault();
string jsonString = JsonSerializer.Serialize(reelAutoChanger);
Console.WriteLine(jsonString);
Console.ReadKey();
}
using (var db = new MachineContext())
{
List<MotorProfile> motorProfiles = db.MotorProfile.ToList();
//IQueryable<MotorProfile> motorProfiles = db.MotorProfile.FromSqlRaw("Select * from MotorProfile");
//var emp = context.Employee.FromSqlRaw("Select * from Employee").OrderBy(x => x.Name).ToList();
string jsonString = JsonSerializer.Serialize(motorProfiles);
Console.WriteLine(jsonString);
Console.ReadKey();
List<Motor> motors = db.Motor.Where(b => b.ModuleName == "PnP").ToList();
jsonString = JsonSerializer.Serialize(motors);
Console.WriteLine(jsonString);
Console.ReadKey();
}
```
Loading…
Cancel
Save