Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

nhibernate g7 #2

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added nhibernateg7/.vs/nhibernateg7/v16/.suo
Binary file not shown.
Empty file.
Binary file not shown.
25 changes: 25 additions & 0 deletions nhibernateg7/nhibernateg7.sln
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 16
VisualStudioVersion = 16.0.28803.352
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "nhibernateg7", "nhibernateg7\nhibernateg7.csproj", "{0E2C1D56-8D94-4424-BC66-3305C1258B38}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{0E2C1D56-8D94-4424-BC66-3305C1258B38}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{0E2C1D56-8D94-4424-BC66-3305C1258B38}.Debug|Any CPU.Build.0 = Debug|Any CPU
{0E2C1D56-8D94-4424-BC66-3305C1258B38}.Release|Any CPU.ActiveCfg = Release|Any CPU
{0E2C1D56-8D94-4424-BC66-3305C1258B38}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {198325B1-DB9F-4E39-AE92-15D81E17D16D}
EndGlobalSection
EndGlobal
6 changes: 6 additions & 0 deletions nhibernateg7/nhibernateg7/App.config
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.7.2" />
</startup>
</configuration>
16 changes: 16 additions & 0 deletions nhibernateg7/nhibernateg7/Car.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace nhibernateg7
{
public class Car
{
public virtual int ID { get; set; }
public virtual string Make { get; set; }
public virtual string Model { get; set; }
public virtual string Year { get; set; }
}
}
20 changes: 20 additions & 0 deletions nhibernateg7/nhibernateg7/CarMapping .cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using NHibernate.Mapping.ByCode;
using NHibernate.Mapping.ByCode.Conformist;
namespace nhibernateg7
{
public class CarMapping:ClassMapping<Car>
{
public CarMapping()
{
Id(s => s.ID, im => im.Generator(Generators.Identity));
Property(s => s.Make, pm => pm.NotNullable(true));
Property(s => s.Model, pm => pm.NotNullable(true));
Property(s => s.Year, pm => pm.NotNullable(true));
}
}
}
16 changes: 16 additions & 0 deletions nhibernateg7/nhibernateg7/Person.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace nhibernateg7
{
public class Person
{
public virtual int ID { get; set; }
public virtual string LastName { get; set; }
public virtual string FirstName { get; set; }
public virtual Car Car { get; set; }
}
}
21 changes: 21 additions & 0 deletions nhibernateg7/nhibernateg7/PersonMapping.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
using NHibernate.Mapping.ByCode;
using NHibernate.Mapping.ByCode.Conformist;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace nhibernateg7
{
public class PersonMapping: ClassMapping<Person>
{
public PersonMapping()
{
Id(s => s.ID, im => im.Generator(Generators.Identity)); // primary key mapping
Property(s => s.FirstName, pm => pm.NotNullable(true));
Property(s => s.LastName, pm => pm.NotNullable(true));
ManyToOne(s => s.Car, mom => mom.Cascade(Cascade.Persist));
}
}
}
138 changes: 138 additions & 0 deletions nhibernateg7/nhibernateg7/ProgramNHibernate.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
using System;
using System.Data;
using System.Linq;
using System.Reflection;

using NHibernate.Cfg;
using NHibernate.Dialect;
using NHibernate.Driver;
using NHibernate.Mapping.ByCode;
using NHibernate.Tool.hbm2ddl;
//using NHibernateDemoApp.Domain;
//using NHibernateDemoApp.Mappings;

using NHibernate.Linq;
using NHibernate.Criterion;
using NHibernate;

namespace nhibernateg7
{
class ProgramNHibernate
{
static void Main(string[] args)
{
var configuration = new Configuration();

configuration.DataBaseIntegration(db =>
{
db.ConnectionString = @"Server=.\sqlexpress;initial catalog=NHibernateTest;Integrated Security=true;";
db.Dialect<MsSql2012Dialect>();
db.Driver<SqlClientDriver>();
});

var modelMapper = new ModelMapper();
modelMapper.AddMapping<PersonMapping>();
modelMapper.AddMapping<CarMapping>();

var mapping = modelMapper.CompileMappingForAllExplicitlyAddedEntities();
configuration.AddDeserializedMapping(mapping, "Test");

var schema = new SchemaExport(configuration);


// schema.Execute(false, true, false);
//ejecutar solo la primera vez

//paso 1
insertarDatos(configuration);
//paso 2
//verDatos(configuration);
// paso 3
// actualizarDatos(configuration)
// paso 4
// deleteDatos(configuration);

}

public static void insertarDatos(Configuration cf)
{
var buildSessionFactory = cf.BuildSessionFactory();
using (var session = buildSessionFactory.OpenSession())
using (var tx = session.BeginTransaction())
{
var person = new Person
{
FirstName = "raul",
LastName = "than",
Car = new Car { Make = "volvawen", Model = "megan", Year = "2013" }
};
session.Save(person);
tx.Commit();

}


}
public static void verDatos(Configuration cf)
{
var buildSessionFactory = cf.BuildSessionFactory();
using (var session = buildSessionFactory.OpenSession())
using (var tx = session.BeginTransaction())
{
var person = session.Query<Person>()
.Where(p => p.FirstName == "Linda")
.Select(p => p.LastName)
.First();

Console.WriteLine(person);
Console.ReadLine();
tx.Commit();
}

}

public static void actualizarDatos(Configuration cf)
{
var buildSessionFactory = cf.BuildSessionFactory();
using (var session = buildSessionFactory.OpenSession())
using (var tx = session.BeginTransaction())
{
//IQuery query = session.CreateQuery("FROM Car WHERE ID = 1");
//Car c = query.List<Car>()[0];
//c.Year= "1988";
//session.Update(c);
//Console.WriteLine(c.Year);
var person = session.QueryOver<Person>().Where(p => p.FirstName == "Linda").List();
person.FirstOrDefault().Car.Year = "1968";
session.Update(person.FirstOrDefault());
Console.WriteLine(person.ToString());
Console.ReadLine();
tx.Commit();

}
}

public static void deleteDatos(Configuration cf)
{

{
var buildSessionFactory = cf.BuildSessionFactory();
using (var session = buildSessionFactory.OpenSession())
using (var tx = session.BeginTransaction())
{

var person = session.QueryOver<Person>().Where(p => p.FirstName == "raul").List();
session.Delete(person.FirstOrDefault());
tx.Commit();

}

}
}

}



}

55 changes: 55 additions & 0 deletions nhibernateg7/nhibernateg7/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
using System.Reflection;
using System.Resources;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Windows;

// La información general de un ensamblado se controla mediante el siguiente
// conjunto de atributos. Cambie estos valores de atributo para modificar la información
// asociada con un ensamblado.
[assembly: AssemblyTitle("nhibernateg7")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("HP Inc.")]
[assembly: AssemblyProduct("nhibernateg7")]
[assembly: AssemblyCopyright("Copyright © HP Inc. 2019")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]

// Si establece ComVisible en false, los tipos de este ensamblado no estarán visibles
// para los componentes COM. Si es necesario obtener acceso a un tipo en este ensamblado desde
// COM, establezca el atributo ComVisible en true en este tipo.
[assembly: ComVisible(false)]

//Para comenzar a compilar aplicaciones que se puedan traducir, establezca
//<UICulture>CultureYouAreCodingWith</UICulture> en el archivo .csproj
//dentro de <PropertyGroup>. Por ejemplo, si utiliza inglés de EE.UU.
//en los archivos de código fuente, establezca <UICulture> en en-US. A continuación, quite la marca de comentario
//del atributo NeutralResourceLanguage. Actualice "en-US" en
//la siguiente línea para que coincida con el valor UICulture del archivo de proyecto.

//[assembly: NeutralResourcesLanguage("en-US", UltimateResourceFallbackLocation.Satellite)]


[assembly: ThemeInfo(
ResourceDictionaryLocation.None, //donde se encuentran los diccionarios de recursos específicos del tema
//(se utiliza si no se encuentra ningún recurso en la página,
// ni diccionarios de recursos de la aplicación)
ResourceDictionaryLocation.SourceAssembly //donde se encuentra el diccionario de recursos genérico
//(se utiliza si no se encuentra ningún recurso en la página,
// aplicación o diccionarios de recursos específicos del tema)
)]


// La información de versión de un ensamblado consta de los cuatro valores siguientes:
//
// Versión principal
// Versión secundaria
// Número de compilación
// Revisión
//
// Puede especificar todos los valores o usar los valores predeterminados de número de compilación y de revisión
// utilizando el carácter "*", como se muestra a continuación:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]
71 changes: 71 additions & 0 deletions nhibernateg7/nhibernateg7/Properties/Resources.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading