I have a very simple sample program to begin to get to grips with the reality of Entity Framework 5. I have read a fair amount of theory and everything seems good but immediately I run into a problem with saving to a database.
The problem I have is that DbContext.SaveChanges() is neither throwing an exception (which i expected) nor saving the data.
Heres the code:
public class PhysicalAddressType
{
[Key]
public long AddressTypeIndexCode { get; set; }
public string TName { get; set; }
public long ClientId { get; set; }
public Guid UserId { get; set; }
public DateTime LastModified { get; set; }
}
public class TestContext : DbContext
{
public TestContext() : base("Name=Basd.ContactManagement") { }
public DbSet Addresses { get; set; }
}
static void Main(string[] args)
{
try
{
using (var ctx = new TestContext())
{
var ph = new PhysicalAddressType();
ph.ClientId = 500;
ph.UserId = Guid.NewGuid();
ph.TName = "test1";
ph.LastModified = DateTime.Now;
ctx.Addresses.Add(ph);
ctx.SaveChanges();
}
}
catch (Exception ex)
{
System.Diagnostics.Debug.WriteLine(ex.Message);
}
}
I expect the code to throw an exception because I have not provided a mapping to the table in the database. Here is the schema of the table:
CREATE TABLE [erp_crm].[PhysicalAddressType](
[AddressTypeIndexCode] [bigint] IDENTITY(1000,1) NOT NULL,
[TName] [nvarchar](50) NOT NULL,
[ClientId] [bigint] NULL,
[UserId] [uniqueidentifier] NULL,
[LastModified] [smalldatetime] NOT NULL,
CONSTRAINT [PK_PhysicalAddressType] PRIMARY KEY CLUSTERED
(
[AddressTypeIndexCode] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
But it doesn't and nor does it store a record in the database.
So my question is how do I debug at this point? My first question would be whether it's even hitting the DB, not because the connection string is wrong (in app.config) but for some other reason. Is "silent failure" a real part of the EF experience?
Answer
My first question would be whether it's even hitting the DB,
You can use SQL Server profiler (or any other custom profiler from independant vendors) for this purpose. It displays all the ative connections and queries, being executed.
I do also suggest that you try reading from the context to check if you hit the db.
So my question is how do I debug at this point?
Have you tried to use step-into framework source debuging feature?
You do also have the source code as your last resort (you can build it and debug your app against produced binaries).
No comments:
Post a Comment