The FOREIGN KEY requirement in Entity Framework clashed with the INSERT statement.

asp.net-mvc c# entity-framework linq visual-studio

Question

I have two tables: PackageType and Subscription. FK is the PackageTypeId for Subs. Now that I'm using EF 4.1 to insert a new record into the Subscription table, an exception is being thrown.

The FOREIGN KEY requirement was in conflict with the INSERT statement."FK_Subscription_PaymentType" The disagreement happened in table "MyDatabaseName" in the database."dbo.PaymentType" an article'Id' The statement has come to an end.

The data in the tables are:

Table of subscriptions:

   Id  (PK)

   PaymentTypeId (FK)

   Period

   date 

PaymentType:

   Id (PK)

   Name

And the Code is described as follows:

    public void Proceed(SubscriptionSessionData data)
    {

        if (data != null)
        {
            PMSCatalogEntities entities = new PMSCatalogEntities();

                Subscription subs = new Subscription();
                subs.Id = Guid.NewGuid();
                subs.ApplicableFrom = data.ApplicableFrom;
                subs.TenantId = tenant.Id;

                subs.PackageId = data.PaymentType;
                subs.PaymentTypeId = data.PaymentType;

                entities.AddToSubscriptions(subs);
                entities.SaveChanges();

        }
  }

Any thoughts on this problem?

1
0
10/19/2011 9:23:02 AM

Accepted Answer

This case has previously been tried, and it works perfectly:

    class Program
    {
        static void Main(string[] args)
        {
            PMSCatalogEntities entities = new PMSCatalogEntities();

            Subscription subs = new Subscription();
            subs.Id = Guid.NewGuid();
            subs.Date = DateTime.Now;
            subs.PaymentId = 1;

            entities.Subscriptions.Add(subs);
            entities.SaveChanges();
        }
    }

    public class PMSCatalogEntities : DbContext
    {
        public DbSet<Subscription> Subscriptions { get; set; }

        public DbSet<Payment> Payments { get; set; }

    }

    public class Subscription
    {
        public Guid Id { get; set; }

        public DateTime Date { get; set; }

        public Payment Payment { get; set; }

        public int PaymentId { get; set; }
    }

    public class Payment
    {
        public int Id { get; set; }

        public string Type { get; set; }
    }

On the DB, too.

TABLE [dbo].[Payments](
    [Id] [int] IDENTITY(1,1) NOT NULL,
    [Type] [nvarchar](max) NULL,

TABLE [dbo].[Subscriptions](
    [Id] [uniqueidentifier] NOT NULL,
    [Date] [datetime] NOT NULL,
    [PaymentId] [int] NOT NULL,

It works well, and all I need to know is that a Payment with the Id 1 in this case is saved on the Payments table.

0
10/19/2011 11:33:03 PM

Popular Answer

You are attempting to insert an incorrect primary key value from a reference table that is not visible in the database.



Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow