Error: Column names in each table must be unique

asp.net c# database entity-framework sql

Question

I am getting an error upon executing update-database which states: "Column names in each table must be unique. Column name 'OrderRecieveDate' in table 'dbo.OrderForms' is specified more than once." I've tried everything I can think of to fix it and nothing has panned out thus far. Previously I was able to add/update/delete rows from my table through the website i've created but now that feature does not even seem to be working. I can't even get the database to add my seed data at this point.

My Context/Models file:

public class OrderFormContext : DbContext
{
    public DbSet<OrderForm> OrderForms { get; set; }
}

public class OrderForm 
{
    [Key, Display(Name= "Order ID" )]
    [ScaffoldColumn(false)]
    public int OrderID { get; set; }


    private DateTime _date = DateTime.Today;
    [Display(Name = "Date Posted")]
    [ScaffoldColumn(false)]
    public DateTime OrderPostDate
    {
        get { return _date; }
        set { _date = value; }
    }

    [EnumDataType(typeof(PersonReqID)),Display(Name = "Person Requesting")]
    public PersonReqID PersonReq { get; set; }

    [StringLength(100), Display(Name = "Grant")]
    public string GrantID { get; set; }

    [StringLength(10000), Display(Name = "Product Description"), DataType(DataType.MultilineText)]
    public string ItemDescription { get; set; }

    [StringLength(100), Display(Name = "Quantity")]
    public string ItemQuantity { get; set; }

    [StringLength(100), Display(Name = "Price")]
    public string UnitPrice { get; set; }

    [StringLength(100), Display(Name = "Company")]
    public string CompanyID { get; set; }

    [StringLength(100), Display(Name = "Catalog #")]
    public string CatalogNum { get; set; }

    [ScaffoldColumn(false)]
    [Range(typeof(DateTime), "1/1/1111", "1/1/3000", ErrorMessage = "Please provide a date.")]
    [DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:d}")]
    public DateTime OrderMadeDate { get; set; }

    [ScaffoldColumn(false)]
    [Range(typeof(DateTime), "1/1/1111", "1/1/3000", ErrorMessage = "Please provide a date.")]
    [DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:d}")]
    public DateTime OrderRecieveDate { get; set; }

    [ScaffoldColumn(false)]
    [EnumDataType(typeof(PersonRecID)), Display(Name = "Person Recieving")]
    public PersonRecID PersonRec { get; set; }

    [ScaffoldColumn(false)]
    [StringLength(100), Display(Name = "Item Location")]
    public string ItemLoc { get; set; }
}

My Configuration.cs file:

context.OrderForms.AddOrUpdate(
                new OrderForm
                {
                    OrderID=1,
                    OrderPostDate=DateTime.Now,
                    PersonReq=PersonReqID.Jake,
                    GrantID="NASA",
                    ItemDescription="Grenades",
                    ItemQuantity="100",
                    UnitPrice="1.50",
                    CompanyID="Grenades UNLMTD",
                    CatalogNum="G1001",
                    PersonRec=PersonRecID.Ajit,
                    ItemLoc="Freezer"
                }

                ); 

         context.SaveChanges();

and finally my xxxxxxx_initial.cs migration file:

public override void Up()
    {
        AddColumn("dbo.OrderForms", "OrderRecieveDate", c =>
         c.DateTime(nullable: false, defaultValue: new DateTime(1000, 1, 1)));
        AddColumn("dbo.OrderForms", "OrderMadeDate", d =>
        d.DateTime(nullable: false, defaultValue: new DateTime(1000, 1, 1)));

    }

    public override void Down()
    {
        DropColumn("dbo.OrderForms", "OrderRecieveDate");
        DropColumn("dbo.OrderForms", "OrderMadeDate");

    }

I'm very new to web design and so i'm just concluding everything that I think could potentially cause an issue. Here is the table definition for my OrderForms.dbo file.

CREATE TABLE [dbo].[OrderForms] (
[OrderID]          INT            IDENTITY (1, 1) NOT NULL,
[GrantID]          NVARCHAR (100) NULL,
[ItemDescription]  NVARCHAR (MAX) NULL,
[ItemQuantity]     NVARCHAR (100) NULL,
[UnitPrice]        NVARCHAR (100) NULL,
[CompanyID]        NVARCHAR (100) NULL,
[CatalogNum]       NVARCHAR (100) NULL,
[OrderDate]        NVARCHAR (100) NULL,
[ItemLoc]          NVARCHAR (100) NULL,
[PersonReq]        INT            DEFAULT ((0)) NULL,
[PersonRec]        INT            DEFAULT ((0)) NULL,
[OrderPostDate]    DATETIME       DEFAULT ('1900-01-01T00:00:00.000') NULL,
[OrderRecieveDate] DATETIME       DEFAULT ('2013-01-01T00:00:00.000') NULL,
[OrderMadeDate]    DATETIME       DEFAULT ('2013-01-01T00:00:00.000') NULL,
CONSTRAINT [PK_dbo.OrderForms] PRIMARY KEY CLUSTERED ([OrderID] ASC)

);

Any help would be greatly appreciated. I can't seem to figure out how the column is being created twice. I have tried deleting the database and previous migrations and doing 'enable-migrations -forced' and restarting from there to no avail. This is probably something that has to do with a lack of background knowledge on EF from my end. Thanks in advance for any help!

1
1
2/10/2014 7:01:06 PM

Popular Answer

So I may be misinterpreting what you have, but dont you create the columns once in the definition:

CREATE TABLE [dbo].[OrderForms] (
[OrderID]          INT            IDENTITY (1, 1) NOT NULL,
[GrantID]          NVARCHAR (100) NULL,
[ItemDescription]  NVARCHAR (MAX) NULL,
[ItemQuantity]     NVARCHAR (100) NULL,
[UnitPrice]        NVARCHAR (100) NULL,
[CompanyID]        NVARCHAR (100) NULL,
[CatalogNum]       NVARCHAR (100) NULL,
[OrderDate]        NVARCHAR (100) NULL,
[ItemLoc]          NVARCHAR (100) NULL,
[PersonReq]        INT            DEFAULT ((0)) NULL,
[PersonRec]        INT            DEFAULT ((0)) NULL,
[OrderPostDate]    DATETIME       DEFAULT ('1900-01-01T00:00:00.000') NULL,
[OrderRecieveDate] DATETIME       DEFAULT ('2013-01-01T00:00:00.000') NULL,
[OrderMadeDate]    DATETIME       DEFAULT ('2013-01-01T00:00:00.000') NULL,
CONSTRAINT [PK_dbo.OrderForms] PRIMARY KEY CLUSTERED ([OrderID] ASC)

Then attempt to create them again

public override void Up()
    {
        AddColumn("dbo.OrderForms", "OrderRecieveDate", c =>
         c.DateTime(nullable: false, defaultValue: new DateTime(1000, 1, 1)));
        AddColumn("dbo.OrderForms", "OrderMadeDate", d =>
        d.DateTime(nullable: false, defaultValue: new DateTime(1000, 1, 1)));

    }

If you need some troubleshooting advice you can use the sys.tables and sys.columns to find the values out, and an IF NOT EXISTS on your add columns.

This will require some modification but it might be enough to get you going.

  string columnToAdd = " ADD [" + columnList[i] + "] " + columnDataTypeList[i];
  string alterTableSQL = @"ALTER TABLE " + DestinationTbl + columnList[i];
  string columnMakerSQL += @"IF NOT EXISTS ( SELECT * FROM   sys.columns WHERE  object_id = OBJECT_ID(N'" + DestinationTbl
                                + @"') AND name = '" + columnList[i]
                                + @"' ) BEGIN "
                                + alterTableSQL
                                + @" END ";
0
3/10/2014 4:22:50 PM


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