Using RecNo() for the primary key is not a preferred practice. But can be useful when you are dealing with third party tables that do not have a primary key.

using System;
using System.Linq;
using IQToolkit;
using IQToolkit.Data.Mapping;
using LinqToVfp;

namespace WindowsFormsApplication1 {
    static class Program {
        [STAThread]
        static void Main() {
            string northwindDbcPath = "your path";
            string connectionString = string.Format("Provider=VFPOLEDB.1;Data Source={0};Exclusive=false;Ansi=true;", northwindDbcPath);

            Northwind nw = new Northwind(connectionString);
            var empt = nw.EmployeeTerritories.Where(d => d.EmployeeTerritoryId == 1).SingleOrDefault();
            
            empt.Employeeid = 99;
            nw.EmployeeTerritories.Update(empt);
            empt = nw.EmployeeTerritories.Where(d => d.EmployeeTerritoryId == 1).SingleOrDefault();
        }
    }

    // Data Context Class
    public partial class Northwind {
        public VfpQueryProvider Provider { get; private set; }

        public Northwind(string connectionString)
            : this(connectionString, typeof(NorthwindAttributes).FullName) {
        }

        public Northwind(string connectionString, string mappingId)
            : this(VfpQueryProvider.Create(connectionString, mappingId)) {
        }

        public Northwind(VfpQueryProvider provider) {
            this.Provider = provider;
        }
        public virtual IEntityTable<EmployeeTerritory> EmployeeTerritories {
            get { return this.Provider.GetTable<EmployeeTerritory>("EmployeeTerritories"); }
        }
    }

    // Attribute Mapping Class
    public partial class NorthwindAttributes  : Northwind {
        public NorthwindAttributes(string connectionString)
            : base(connectionString) {
        }

        [Table()]
        [Column(Member = "EmployeeTerritoryId", Name = "Recno()", IsPrimaryKey = true, IsGenerated = true)]
        [Column(Member = "EmployeeId")]
        [Column(Member = "TerritoryId")]
        public override IEntityTable<EmployeeTerritory> EmployeeTerritories {
            get { return base.EmployeeTerritories; }
        }
    }

    // Entity Class
    public partial class EmployeeTerritory {
        public int EmployeeTerritoryId { get; set; }
        public int Employeeid { get; set; }
        public string Territoryid { get; set; }
    }
}

Last edited Apr 18, 2010 at 2:03 AM by TomBrothers, version 5

Comments

No comments yet.