public EntityWriter(DatabaseSchema schema, CodeWriterSettings codeWriterSettings) { this.schema = schema; this.codeWriterSettings = codeWriterSettings; PrepareSchemaNames.Prepare(schema, this.codeWriterSettings.Namer); dataAnnotationWriter = new DataAnnotationWriter(false, codeWriterSettings); mappingNamer = new MappingNamer(); }
public void MappingSharedPrimaryKeyTest() { //arrange var schema = new DatabaseSchema(null, null); schema .AddTable("vehicle") .AddColumn<string>("regnum").AddPrimaryKey().AddLength(25) .AddColumn<string>("model").AddLength(32) .AddTable("car") .AddColumn<string>("regnum").AddLength(25).AddPrimaryKey().AddForeignKey("fk", "vehicle") .AddColumn<int>("doors"); //make sure it's all tied up DatabaseSchemaFixer.UpdateReferences(schema); //make sure .Net names are assigned PrepareSchemaNames.Prepare(schema, new Namer()); var table = schema.FindTableByName("car"); var mappingNamer = new MappingNamer(); var codeWriterSettings = new CodeWriterSettings{ CodeTarget = CodeTarget.PocoEntityCodeFirst }; var target = new CodeFirstMappingWriter(table, codeWriterSettings, mappingNamer); //act var txt = target.Write(); //assert var hasScalarKey = txt.Contains("HasKey(x => x.Regnum);"); var hasForeignKey = txt.Contains("HasRequired(x => x.Vehicle);"); Assert.IsTrue(hasScalarKey); Assert.IsTrue(hasForeignKey); }
public void MappingNameWithConflictsTest() { //arrange var schema = new DatabaseSchema(null, null); var table = schema.AddTable("Products") .AddColumn("ProductId", DbType.Int32).AddPrimaryKey().AddIdentity() .Table; table.NetName = "Product"; //we need datatypes schema.DataTypes.Add(new DataType("INT", "System.Int32")); //make sure it's all tied up DatabaseSchemaFixer.UpdateReferences(schema); var mappingNamer = new MappingNamer(); var target = new CodeFirstMappingWriter(table, new CodeWriterSettings(), mappingNamer); //we also have a table called "ProductMapping" //so we can't call the mapping class for "Product" the same name mappingNamer.EntityNames.Add("ProductMapping"); mappingNamer.EntityNames.Add("ProductMappingMap"); //act target.Write(); //now the name is assigned var className = target.MappingClassName; //assert Assert.AreEqual("ProductMappingMapMap", className, "Should not conflict with the existing names"); Assert.AreEqual(3, mappingNamer.EntityNames.Count, "Should add the name to the list"); }
/// <summary> /// Uses the specified schema to write class files, NHibernate/EF CodeFirst mapping and a project file. Any existing files are overwritten. If not required, simply discard the mapping and project file. Use these classes as ViewModels in combination with the data access strategy of your choice. /// </summary> /// <param name="directory">The directory to write the files to. Will create a subdirectory called "mapping". The directory must exist- any files there will be overwritten.</param> /// <exception cref="ArgumentNullException"/> /// <exception cref="InvalidOperationException"/> /// <exception cref="IOException"/> /// <exception cref="UnauthorizedAccessException"/> /// <exception cref="System.Security.SecurityException" /> public void Execute(DirectoryInfo directory) { if (directory == null) throw new ArgumentNullException("directory"); if (!directory.Exists) throw new InvalidOperationException("Directory does not exist: " + directory.FullName); var pw = CreateProjectWriter(); InitMappingProjects(directory, pw); _mappingNamer = new MappingNamer(); foreach (var table in _schema.Tables) { if (FilterIneligible(table)) continue; var className = table.NetName; UpdateEntityNames(className, table.Name); var cw = new ClassWriter(table, _codeWriterSettings); var txt = cw.Write(); var fileName = WriteClassFile(directory, className, txt); pw.AddClass(fileName); WriteMapping(table, pw); } if (_codeWriterSettings.IncludeViews) { foreach (var view in _schema.Views) { var className = view.NetName; UpdateEntityNames(className, view.Name); var cw = new ClassWriter(view, _codeWriterSettings); var txt = cw.Write(); var fileName = WriteClassFile(directory, className, txt); pw.AddClass(fileName); WriteMapping(view, pw); } } string contextName = null; if (IsCodeFirst()) { contextName = WriteDbContext(directory, pw); } //we could write functions (at least scalar functions- not table value functions) //you have to check the ReturnType (and remove it from the arguments collections). if (_codeWriterSettings.WriteStoredProcedures) { WriteStoredProcedures(directory.FullName, pw); WritePackages(directory.FullName, pw); } if (_codeWriterSettings.WriteUnitTest) WriteUnitTest(directory.FullName, contextName); WriteProjectFile(directory, pw); }
/// <summary> /// Uses the specified schema to write class files, NHibernate/EF CodeFirst mapping and a project file. Any existing files are overwritten. If not required, simply discard the mapping and project file. Use these classes as ViewModels in combination with the data access strategy of your choice. /// </summary> /// <param name="directory">The directory to write the files to. Will create a subdirectory called "mapping". The directory must exist- any files there will be overwritten.</param> /// <exception cref="ArgumentNullException"/> /// <exception cref="InvalidOperationException"/> /// <exception cref="IOException"/> /// <exception cref="UnauthorizedAccessException"/> /// <exception cref="System.Security.SecurityException" /> public void Execute(DirectoryInfo directory) { if (directory == null) { throw new ArgumentNullException("directory"); } if (!directory.Exists) { throw new InvalidOperationException("Directory does not exist: " + directory.FullName); } var pw = CreateProjectWriter(); InitMappingProjects(directory, pw); _mappingNamer = new MappingNamer(); foreach (var table in _schema.Tables) { if (FilterIneligible(table)) { continue; } var className = table.NetName; UpdateEntityNames(className, table.Name); var cw = new ClassWriter(table, _codeWriterSettings); var txt = cw.Write(); var fileName = WriteClassFile(directory, className, txt); pw.AddClass(fileName); WriteMapping(table, pw); } if (_codeWriterSettings.IncludeViews) { foreach (var view in _schema.Views) { var className = view.NetName; UpdateEntityNames(className, view.Name); var cw = new ClassWriter(view, _codeWriterSettings); var txt = cw.Write(); var fileName = WriteClassFile(directory, className, txt); pw.AddClass(fileName); WriteMapping(view, pw); } } string contextName = null; if (IsCodeFirst()) { contextName = WriteDbContext(directory, pw); } //we could write functions (at least scalar functions- not table value functions) //you have to check the ReturnType (and remove it from the arguments collections). if (_codeWriterSettings.WriteStoredProcedures) { WriteStoredProcedures(directory.FullName, pw); WritePackages(directory.FullName, pw); } if (_codeWriterSettings.WriteUnitTest) { WriteUnitTest(directory.FullName, contextName); } WriteProjectFile(directory, pw); }