Ejemplo n.º 1
0
        public void AssociationMapping()
        {
            var mapping = new DataContextMapping <AccessNorthwind>();

            mapping.Provider = new ProviderAttribute(typeof(AccessDbProvider));

            var categoryMapping = new EntityMapping <Category>();

            categoryMapping.TableAttribute = new TableAttribute {
                Name = "Categories"
            };
            categoryMapping.Column(o => o.CategoryID, new ColumnAttribute {
                IsPrimaryKey = true
            });
            categoryMapping.Column(o => o.CategoryName);
            categoryMapping.Association(o => o.Products, new AssociationAttribute {
                OtherKey = "CategoryID"
            });
            mapping.Add(categoryMapping);

            var productMapping = new EntityMapping <Product>();

            productMapping.TableAttribute = new TableAttribute {
                Name = "Products"
            };
            productMapping.Column(o => o.ProductID, o => o.PrimaryKey());
            productMapping.Column(o => o.ProductName, o => o.NeverUpdateCheck());
            productMapping.Column(o => o.CategoryID, o => o.NeverUpdateCheck());
            productMapping.Association(o => o.Category, o => o.Keys("CategoryID", "CategoryID").Storage("_Category"));
            mapping.Add(productMapping);

            var mappingSource = new FluentMappingSource(o => mapping);
            //mappingSource.Add(mapping);

            var db = new AccessNorthwind(@"C:/Northwind.mdb", mappingSource)
            {
                Log = Console.Out
            };

            db.Categories.Select(o => new { o.CategoryID, o.CategoryName, o.Products }).ToArray();

            db.Categories.ToArray();
            db.Products.ToArray();

            var products = db.Products.ToList();

            products.ForEach(o => Console.WriteLine(o.Category));
        }
Ejemplo n.º 2
0
        public void DynamicColumGroup()
        {
            var mappingSource = new FluentMappingSource(delegate
            {
                var contextMapping      = new DataContextMapping <DataContext>();
                contextMapping.Provider = new ProviderAttribute(typeof(ALinq.MySQL.MySqlProvider));

                var employeeMapping = new EntityMapping <MyEmployee>();
                employeeMapping.Table("Employees")
                .Column(o => o["EmployeeID"], o => o.PrimaryKey().AutoSyncOnInsert())
                .Column(o => o["City"])
                .Column(o => o["FirstName"])
                .Column(o => o["LastName"]);

                contextMapping.Add(employeeMapping);
                return(contextMapping);
            });

            var conn  = "server=localhost;User Id=root;Password=test;Persist Security Info=True;database=northwind";
            var db    = new DataContext(conn, mappingSource);
            var table = db.GetTable <MyEmployee>();

            var keys1 = table.GroupBy(o => o["City"])
                        .Select(o => o.Key);

            keys1.ToList().ForEach(Console.WriteLine);

            var keys2 = table.GroupBy(o => new { F = o["FirstName"], L = o["LastName"] })
                        .Select(o => o.Key);

            keys2.ToList().ForEach(Console.WriteLine);
        }
Ejemplo n.º 3
0
        public void DynamicProperty2()
        {
            var mappingSource = new FluentMappingSource(delegate
            {
                var contextMapping      = new DataContextMapping <DataContext>();
                contextMapping.Provider = new ProviderAttribute(typeof(ALinq.MySQL.MySqlProvider));

                var employeeMapping = new EntityMapping <MyEmployee>();
                employeeMapping.Table("Employees")
                .Column(o => o["EmployeeID"], o => o.PrimaryKey().AutoSyncOnInsert())
                .Column(o => o["City"])
                .Column(o => o["FirstName"])
                .Column(o => o["LastName"]);

                contextMapping.Add(employeeMapping);
                return(contextMapping);
            });

            var conn = "server=localhost;User Id=root;Password=test;Persist Security Info=True;database=northwind";
            var db   = new DataContext(conn, mappingSource)
            {
                Log = Console.Out
            };

            var table = db.GetTable <MyEmployee>();

            table.Select(o => o["FirstName"]).Cast <dynamic>().ToArray();
            table.Select("['FirstName']").Cast <dynamic>().ToArray();

            table.Select(o => new { OrderDate = o["FirstName"] }).ToArray();
            table.Select("new (['FirstName'] as F, ['LastName'] as L)").Cast <dynamic>().ToList()
            .ForEach(o => Console.WriteLine(o.F + " " + o.L));
        }
Ejemplo n.º 4
0
        public void GetDataContextMapping()
        {
            var mapping = new DataContextMapping <AccessNorthwind>();

            mapping.Provider = new ProviderAttribute(typeof(AccessDbProvider));

            var categoryMapping = new EntityMapping <Category>();

            categoryMapping.TableAttribute = new TableAttribute {
                Name = "Categories"
            };
            categoryMapping.Column(o => o.CategoryID, o => o.PrimaryKey());
            categoryMapping.Column(o => o.CategoryName, o => o.NeverUpdateCheck());
            categoryMapping.Association(o => o.Products, o => o.Keys("CategoryID", "CategoryID"));
            mapping.Add(categoryMapping);

            var mappingSource = new FluentMappingSource(o => mapping);
            //mappingSource.Add(mapping);

            //mapping = mappingSource.GetDataContextMapping<AccessNorthwind>();
            //Assert.IsNotNull(mapping);

            //var mapping1 = mappingSource.GetDataContextMapping<DataContext>();
            //Assert.IsNull(mapping1);
        }
Ejemplo n.º 5
0
        public void ColumnMapping()
        {
            var mapping = new DataContextMapping <AccessNorthwind>();

            mapping.Provider = new ProviderAttribute(typeof(AccessDbProvider));

            var categoryMapping = new EntityMapping <Category>();

            categoryMapping.TableAttribute = new TableAttribute {
                Name = "Categories"
            };
            categoryMapping.Column(o => o.CategoryID);
            categoryMapping.Column(o => o.CategoryName);
            mapping.Add(categoryMapping);

            var mappingSource = new FluentMappingSource(o => mapping);
            //mappingSource.Add(mapping);

            var db = new AccessNorthwind(@"C:/Northwind.mdb", mappingSource)
            {
                Log = Console.Out
            };

            db.Categories.Select(o => new { o.CategoryID, o.CategoryName }).ToArray();
        }
Ejemplo n.º 6
0
        public void Test1()
        {
            Exception myexc = null;

            try
            {
                var mapping = new DataContextMapping <AccessNorthwind>();
                mapping.Provider = new ProviderAttribute(typeof(AccessDbProvider));

                var categoryMapping = new EntityMapping <Category>();
                categoryMapping.Column(o => o.CategoryID);
                categoryMapping.Column(o => o.CategoryName);
                categoryMapping.Column(o => o.Products);
                mapping.Add(categoryMapping);

                var mappingSource = new FluentMappingSource(o => mapping);
                //mappingSource.Add(mapping);

                var db = new AccessNorthwind(@"C:/Northwind.mdb", mappingSource);
                db.Categories.Select(o => new { o.CategoryID, o.CategoryName, Products = o.Products.ToArray() }).ToArray();
            }
            catch (Exception exc)
            {
                myexc = exc;
            }
            Assert.IsNotNull(myexc);
            Console.WriteLine(myexc.Message);
        }
Ejemplo n.º 7
0
        public void Test3()
        {
            var mappingSource = new FluentMappingSource(delegate
            {
                var contextMapping      = new DataContextMapping <DataContext>();
                contextMapping.Provider = new ProviderAttribute((typeof(AccessDbProvider)));

                var employeeMapping = new EntityMapping <MyEmployee>();
                employeeMapping.Table("Employees")
                .Column(o => (string)o["City"])
                .Column(o => (string)o["FirstName"])
                .Column(o => (string)o["LastName"]);

                contextMapping.Add(employeeMapping);
                return(contextMapping);
            });
            var dc        = new DataContext("C:/Northwind.mdb", mappingSource);
            var metaTable = dc.Mapping.GetTable(typeof(MyEmployee));

            Assert.IsNotNull(metaTable);
        }