Example 1

This example shows how to create a database with two tables.

After the execution you will have a working database and a dll containing your Code First Schema ready for use as you have manually written it!!

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using EFMagic;
using EFMagic.Schema;
namespace Test
{
    class Program
    {
        static void Main(string[] args)
        {
            EFMColumn CustomerId = new EFMColumn()
            {
                Id = new Guid("6A337629-65C4-45D1-9145-D9B4A5780851"),
                Name = "Id",
                DBName = "colId",
                DataType = "Guid",
                DBDataType = "uniqueidentifier",
                AdditionalParameters = "NOT NULL"
            };
            EFMColumn CustomerName = new EFMColumn()
            {
                Id = new Guid("0097035C-BC9F-4414-B3EC-CE26B3C80DA4"),
                Name = "Name",
                DBName = "colName",
                DataType = "String",
                DBDataType = "nvarchar(20)",
                AdditionalParameters = "NOT NULL"
            };
            EFMColumn CustomerSurname = new EFMColumn()
            {
                Id = new Guid("5AF4B4CD-5ABD-40FD-9B1B-B2A23E494CC0"),
                Name = "Surname",
                DBName = "colSurname",
                DataType = "String",
                DBDataType = "nvarchar(30)"
            };
            EFMPrimaryKey PK_CustomerID = new EFMPrimaryKey(){
                Name = "PK_CustomerID",
                ColumnId = new Guid("6A337629-65C4-45D1-9145-D9B4A5780851")
            };
            EFMIndex Ind_CustomerName = new EFMIndex() {
                Name = "Ind_CustomerName",
                ColumnId = new Guid("0097035C-BC9F-4414-B3EC-CE26B3C80DA4")
            };
            EFMUnique Un_CustomerSurname = new EFMUnique() {
                Name = "Un_CustomerSurname",
                ColumnId = new Guid("0097035C-BC9F-4414-B3EC-CE26B3C80DA4")
            };
            EFMTable Customer = new EFMTable()
            {
                Id = new Guid("F53D64E3-89E5-4A77-AE5E-7ECFD7CE2FDA"),
                Name = "Customer",
                DBName = "tblCustomers"
            };
            Customer.Columns.Add(CustomerId.Id, CustomerId);
            Customer.Columns.Add(CustomerName.Id, CustomerName);
            Customer.Columns.Add(CustomerSurname.Id, CustomerSurname);
            Customer.Constraints.Add(PK_CustomerID.Name,PK_CustomerID);
            Customer.Constraints.Add(Un_CustomerSurname.Name,Un_CustomerSurname);
            Customer.Indexes.Add(Ind_CustomerName.Name,Ind_CustomerName);

            EFMColumn EmailId = new EFMColumn()
            {
                Id = new Guid("36FBC38E-D2E9-49DC-B685-F4D2149CFCB5"),
                Name = "Id",
                DBName = "colId",
                DataType = "Guid",
                DBDataType = "uniqueidentifier",
                AdditionalParameters = "NOT NULL"
            };
            EFMColumn EmailCustomerId = new EFMColumn()
            {
                Id = new Guid("BFD13C42-182C-4303-89DD-5538BFC80DFA"),
                Name = "CustomerId",
                DBName = "colCustomerId",
                DataType = "Guid",
                DBDataType = "uniqueidentifier",
                AdditionalParameters = "NOT NULL"
            };
            EFMColumn EmailAddress = new EFMColumn()
            {
                Id = new Guid("7E19F650-63E7-4F28-86A6-78575CECE7AC"),
                Name = "Name",
                DBName = "colName",
                DataType = "String",
                DBDataType = "nvarchar(20)",
                AdditionalParameters = "NOT NULL"
            };
            EFMPrimaryKey PK_EmailId = new EFMPrimaryKey()
            {
                Name = "PK_EmailId",
                ColumnId = new Guid("36FBC38E-D2E9-49DC-B685-F4D2149CFCB5")
            };
            EFMForeignKey FK_EmailCustomerId = new EFMForeignKey()
            {
                Name = "FK_EmailCustomerId",
                ColumnId = new Guid("BFD13C42-182C-4303-89DD-5538BFC80DFA"),
                RTableId = new Guid("F53D64E3-89E5-4A77-AE5E-7ECFD7CE2FDA"),
                RColumnId = new Guid("6A337629-65C4-45D1-9145-D9B4A5780851")
            };
            EFMTable Email = new EFMTable()
            {
                Id = new Guid("522266DE-9587-4529-9D8E-A24AE74D85F6"),
                Name = "Email",
                DBName = "tblEmails"
            };
            Email.Columns.Add(EmailId.Id, EmailId);
            Email.Columns.Add(EmailCustomerId.Id, EmailCustomerId);
            Email.Columns.Add(EmailAddress.Id, EmailAddress);
            Email.Constraints.Add(PK_EmailId.Name, PK_EmailId);
            Email.Constraints.Add(FK_EmailCustomerId.Name, FK_EmailCustomerId);
            EFMSchema CustomerManagement = new EFMSchema()
            {
                Name = "CustomerManagement",
                Version = 1
            };
            CustomerManagement.Tables.Add(Customer.Id,Customer);
            CustomerManagement.Tables.Add(Email.Id,Email);
            Console.Write("Server: ");
            string Server = Console.ReadLine();
            Console.Write("Database: ");
            string Database = Console.ReadLine();
            Console.Write("Username: ");
            string Username = Console.ReadLine();
            Console.Write("Password: ");
            string Password = Console.ReadLine();
            EFMSQLSchemaGenerator SqlSchemaManager = new EFMSQLSchemaGenerator(CustomerManagement, new EFMSchema());
            SqlSchemaManager.UpdateDatabase(String.Format("Data Source={0};User Id={1};Password={2};Initial Catalog={3}", Server,Username,Password,Database));
            Console.Write("Enter the filename to save your dll: ");
            string Filename = Console.ReadLine();
            EFMCodeFirstGenerator CodeFirst = new EFMCodeFirstGenerator(CustomerManagement, @"C:\Program Files (x86)\Microsoft ADO.NET Entity Framework 4.1 RC\Binaries\");
            CodeFirst.CompileToFile(Filename);
        }
    }
}

Last edited Jun 13, 2011 at 11:16 AM by ikaragkiozoglou, version 2

Comments

No comments yet.