Using Entity Framework with InterSystems IRIS Data Platform
Learn to use Entity Framework, an object/relational mapping tool, to connect your .NET application to InterSystems IRIS® data platform. Entity Framework is used alongside the built-in technologies ADO.Net, XEP, and Native API. Entity Framework allows you to map your .NET objects to relational tables in InterSystems IRIS and eliminates the need for most of the data-access code that developers usually need to write. With a bit of prior setup, you can use it with InterSystems IRIS as you would with any other database. Note the following information about using Entity Framework with InterSystems IRIS:
- These Entity Frameworks tutorial videos are designed for .NET Framework. Entity Framework in .NET Core is not supported.
- Follow Setting Up the Entity Framework Provider in the documentation to install the InterSystems IRIS Entity Framework Provider distribution.
- Then, you can add a new connection string to App.config, specifying iris as the SQLDIALECT and using InterSystems.Data.IrisClient as the providerName.
The following videos will walk through an example using Entity Framework with InterSystems IRIS, including setting up the InterSystems IRIS Entity Framework Provider, using both the Code First and Database First approaches. You can also use Entity Framework in a Model First approach, but that will not be covered.
1. Using Entity Framework with InterSystems IRIS: Overview and Setup
Code Used in the "Overview and Setup" Video
Reference Timestamp Code Allow InterSystems IRIS Entity Framework Provider to be accessed by Visual Studio 2:20 devenv /setup
2. Using Entity Framework with InterSystems IRIS: Code First
Code Used in the "Code First" Video
Reference Timestamp Code Connection string in the App.config
file1:39 Begin code: <connectionStrings> <add name="HRDBConnectionString" connectionString="SERVER = localhost; NAMESPACE = USER; port=51776; METADATAFORMAT = mssql; USER = _SYSTEM; password = SYS; LOGFILE = C:\\Users\\Public\\logs\\cprovider.log; SQLDIALECT = iris;" providerName="InterSystems.Data.IrisClient" /> </connectionStrings>
End codeReference to the Address class within the Employee class 2:49 public virtual Address Address { get; set; }
Create a context class, which inherits from dbContext 2:55 public class EmpContext: DbContext {}
Set the data source for the Context class to the data source defined in App.config
3:00 public EmpContext() : base("name-HRDBConnectionString") {}
Define a DbSet for a data class 3:05 public DbSet<Employee> Employees { get; set; }
Create a migration file from the package manager consoler 3:40 enable-migrations
Main method to create a new employee and address, stored to InterSystems IRIS 4:13 Begin code: static void Main(string[] args) { using (var ctx = new EmpContext()) { Address add = new Address() { Street = "1 Main St.", City = "Cambridge", State = "MA", Zip = "02144" }; Employee emp = new Employee() { EmpName = "Test employee", Address = add }; ctx.Employees.Add(emp); ctx.SaveChanges(); } Console.WriteLine("Employee Saved! Press any key to continue."); Console.ReadKey(); } }
End codeGet a value from the user 4:34 Console.WriteLine("What is the name of the new employee?");
String empName = Console.ReadLine();Query for all employees that start with the letter J 4:53 var employees = from b in ctx.Employees
where b.EmpName.StartsWith('J')
select b;3. Using Entity Framework with InterSystems IRIS: Database First
Code Used in the "Database First" Video
Reference Timestamp Code Loop through employees and display them to the screen 3:34 Begin code: using (var ctx = new localhostEntities()) { //Read var employees = ctx.Employees; foreach (var employee in employees) { Console.WriteLine("Employee " + employee.ID + ": " + employee.Name); } }
End codeQuery for all employees that start with the letter E 3:46 var employees = from b in ctx.Employees where b.EmpName.StartsWith('E') select b;
Save a new employee to the database 4:01 Employee emp = new Employee() { EmpName = "Test employee", Address = add }; ctx.Employees.Add(emp); ctx.SaveChanges();
Mark me "complete" for these videos.