Entity Framework 4.1 code first with a DB2 Linked Server in SQL
I would like to use Entity Framework 4.1 Code-First to build connections to my databases whether they are on our SQL server or DB2. I feel like I'm almost there, but just can't get it to work.
We're using SQL 2005 and have a Linked Server setup to our DB2 mainframe called ISERIES DATA.
Here's my entity object:
using System;
using System.ComponentModel.DataAnnotations;
namespace HOP.Infrastructure.Model
{
[Table("EDIHA101_DEMO")]
public class EDIHA101
{
[Key, Column(Order = 0)]
public int HABCH { get; set; }
[Key, Column(Order = 1)]
public int HABSQ { get; set; }
public int HAVND { get; set; }
public string HACUS { get; set; }
public string HAPRD { get; set; }
public string HADSC { get; set; }
public string HAUOM { get; set; }
public string HADCK { get; set; }
public string HALDT { get; set; }
public string HALTM开发者_开发技巧 { get; set; }
public int HAQQY { get; set; }
public int HACQY { get; set; }
public int HAOQY { get; set; }
public int HAPON { get; set; }
public string HACMT { get; set; }
public string HAADT { get; set; }
public string HAATM { get; set; }
public string HADTP { get; set; }
public string HADID { get; set; }
public int HAALN { get; set; }
public int HAMDL { get; set; }
public int HAAS1 { get; set; }
public string HARDT { get; set; }
public string HARTM { get; set; }
public int HARQY { get; set; }
public int HANQY { get; set; }
public int HADBL { get; set; }
public int HARSQ { get; set; }
public int HAASN { get; set; }
public int HABOL { get; set; }
public string HADWG { get; set; }
public string HAREV { get; set; }
public string HACBY { get; set; }
public string HACDT { get; set; }
public string HACTM { get; set; }
public string HAFDT { get; set; }
public string HAFTM { get; set; }
public string HACD1 { get; set; }
}
}
The Table attribute on the class is pointing to a VIEW that I had created to expose the DB2 table. I can run SELECT queries against this view from SQL management studio and it returns data.
Here's the data context being used:
using System.Data.Entity;
namespace HOP.Infrastructure.Model
{
public class EDIDataContext : DbContext
{
public DbSet<EDIHA101> EDIHA101 { get; set; }
public EDIDataContext(string connectionString)
: base(connectionString)
{
}
}
}
Here's the repository with the Create method that is trying to create a new record:
using HOP.Core.Interfaces;
using HOP.Infrastructure.Mappings;
using HOP.Infrastructure.Model;
using Domain = HOP.Core.Domain;
using Model = HOP.Infrastructure.Model;
namespace HOP.Infrastructure.Repositories
{
public class ItemRepository : IItemRepository
{
protected EDIDataContext db;
public ItemRepository(EDIDataContext db)
{
this.db = db;
}
public void Create(Domain.Item item)
{
Model.EDIHA101 edi = item.Map();
db.EDIHA101.Add(edi);
db.SaveChanges();
}
}
}
Here's the connection string:
<add name="Development_AS400" connectionString="Data Source=SERVER_NAME;Initial Catalog=ISERIES DATA;Integrated Security=True;MultipleActiveResultSets=True" providerName="System.Data.SqlClient" />
When I execute the Create method in the repository code, I get this exception on the db.SaveChanges() line:
"The requested operation could not be performed because OLE DB provider \"DB2OLEDB\" for linked server \"ISERIES\" does not support the required transaction interface."
I found this post that suggests turning on Journaling on DB2:
http://www.sqlservercentral.com/Forums/Topic265107-289-1.aspx
Our DB2 DBA does not want to enable Journaling, so can I disable transactions somehow in EF? Or, is there something else I can try?
Thanks!
Is there any reason you can't connect to the server directly. The error you are receiving suggests an unsupported data provider not journaling my only other suggestion I can think of is using replication instead of linked servers as am not 100% sure that linked servers / oledb or odac is directly supported out of the box in ef4.1 I know there are some 3rd party providers such as dot connect from devart that will support these however they do have an additional expense attached to them
精彩评论