Fluent Migration ForeignKey Error

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.
1 anno tempo fa
Hello,
I'm trying to create two tables (the two tables are related) using Nopcommerce 4.50 Fluent Migration. I specify the Location Id field in my first table as a foreign key (as in the source https://docs.nopcommerce.com/en/developer/plugins/plugin-with-data-access.html) but I am getting the following error. What should I do at this stage? I would be glad if you help
This is my code: table.WithColumn(nameof(TableManagementTrackerRecord.Id)).AsInt32().PrimaryKey() .WithColumn(nameof(TableManagementTrackerRecord.LocationId)).AsInt32().ForeignKey<LocationTrackerRecord>(onDelete: Rule.WithCcaden) (nameof(TableManagementTrackerRecord.HtmlString)).AsString(400) .WithColumn(nameof(TableManagementTrackerRecord.Name)).AsString() .WithColumn(nameof(TableManagementTrackerRecord.Description)).AsString();

This is the error I got: System.Exception: 'An error occurred executing the following sql:
CREATE TABLE [dbo].[TableManagementTrackerRecord] ([Id] INT NOT NULL, [HtmlString] NVARCHAR(400) NOT NULL, [Name] NVARCHAR(255) NOT NULL, [Description] NVARCHAR(255) NOT NULL, [Id] INT NOT NULL, [LocationId] INT NOT NULL, CONSTRAINT [PK_TableManagementTrackerRecord] PRIMARY KEY ([Id], [Id]))
The error was Column names in each table must be unique. Column name 'Id' in table 'TableManagementTrackerRecord' is specified more than once.
'
Column names in each table must be unique. Column name 'Id' in table 'TableManagementTrackerRecord' is specified more than once.
1 anno tempo fa
Gulsen wrote:

Column names in each table must be unique. Column name 'Id' in table 'TableManagementTrackerRecord' is specified more than once.


I think the problem is you dont need to add table.WithColumn(nameof(TableManagementTrackerRecord.Id)).AsInt32().PrimaryKey()
The system will probably add the Id automatically
For example
See src\Plugins\Nop.Plugin.Shipping.FixedByWeightByTotal\Data\ShippingByWeightByTotalRecordBuilder.cs
Does not define the Id
1 anno tempo fa
HI
as Yidna said refer to the
For example See src\Plugins\Nop.Plugin.Shipping.FixedByWeightByTotal\Data\ShippingByWeightByTotalRecordBuilder.cs
Does not define the Id

and you need to add a foreign key like this

  public override void MapEntity(CreateTableExpressionBuilder table)
        {
            table
                 .WithColumn(nameof(ProductDetail.ProductId)).AsInt32().ForeignKey<Product>();
        }

{
            table
                 .WithColumn(nameof(Foreign Key)).AsInt32().ForeignKey<foregin key table >();
        }


no need to add declared primary key directly you can add the foreign key and its table name
1 anno tempo fa
ilyas_patel wrote:

  public override void MapEntity(CreateTableExpressionBuilder table)
        {
            table
                 .WithColumn(nameof(ProductDetail.ProductId)).AsInt32().ForeignKey<Product>();
        }

{
            table
                 .WithColumn(nameof(Foreign Key)).AsInt32().ForeignKey<foregin key table >();
        }


no need to add declared primary key directly you can add the foreign key and its table name


You can also set rule for  OnDelete().
None =  No action taken on related rows.
Cascade = Delete or update related rows. This is the default.
SetNull = Set values in related rows to DBNull.
SetDefault = Set values in related rows to the value contained in the System.Data.DataColumn.DefaultValue property.

example:
 table
                 .WithColumn(nameof(Foreign Key)).AsInt32().ForeignKey<foregin key table >().OnDelete(Rule.None);
1 anno tempo fa
Thank you everyone,
When I try like that, I get the following error

An error occured executing the following sql:
ALTER TABLE [dbo].[TableManagementTrackerRecord] ADD CONSTRAINT [FK_TableManagementTrackerRecord_LocationId_LocationTrackerRecord_Id] FOREIGN KEY ([LocationId]) REFERENCES [dbo].[LocationTrackerRecord] ([Id]) ON DELETE CASCADE
The error was Foreign key 'FK_TableManagementTrackerRecord_LocationId_LocationTrackerRecord_Id' references invalid table 'dbo.LocationTrackerRecord'.
Could not create constraint or index. See previous errors.

first model:
public class TableManagementTrackerRecord : BaseEntity
    {      
        public int LocationId { get; set; }
        public string HtmlString  { get; set; }
        public string Name { get; set; }      
        public string Description { get; set; }
    }
second model:
public class LocationTrackerRecord : BaseEntity, ILocalizedEntity
    {
            public string Location { get; set; }
    }

Mapping class:
  table                        .WithColumn(nameof(TableManagementTrackerRecord.LocationId)).AsInt32().ForeignKey<LocationTrackerRecord>(onDelete: Rule.Cascade)  
                .WithColumn(nameof(TableManagementTrackerRecord.HtmlString)).AsString(400)
                .WithColumn(nameof(TableManagementTrackerRecord.Name)).AsString(100)
                .WithColumn(nameof(TableManagementTrackerRecord.Description)).AsString(200);

Migration Class:
public override void Up()
        {
            Create.TableFor<TableManagementTrackerRecord>();
            Create.TableFor<LocationTrackerRecord>();            
        }
1 anno tempo fa
Hello Gulsen,

You need to first create LocationTrackerRecord table then TableManagementTrackerRecord table.

Migration Class:
public override void Up()
        {
            Create.TableFor<LocationTrackerRecord>();
            Create.TableFor<TableManagementTrackerRecord>();          
        }
1 anno tempo fa
Thank you very much SagarKayasth,  this solved my problem. But I couldn't understand the logic, why does it have to be like this?
1 anno tempo fa
Gulsen wrote:
Thank you very much SagarKayasth,  this solved my problem. But I couldn't understand the logic, why does it have to be like this?


You wrote first TableManagementTrackerRecord table but your LocationId field relation with LocationTrackerRecord table's Id field and LocationTrackerRecord table isn't create first because you wrote create table script after TableManagementTrackerRecord table.
1 anno tempo fa
Understood. Thank you again
This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.