Skip to content

Instantly share code, notes, and snippets.

@jpolvora
Created November 10, 2011 14:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jpolvora/1354920 to your computer and use it in GitHub Desktop.
Save jpolvora/1354920 to your computer and use it in GitHub Desktop.
Utilizando Entity Framework Code First 4.1 c/ ORACLE
//POST: http://silverlightrush.blogspot.com/2011/11/receita-de-bolo-para-ef-code-first-e.html
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Data.Common;
using System.Data.Entity;
using System.Data.Entity.Infrastructure;
using System.Linq;
using Oracle.DataAccess.Client;
namespace ConsoleApplication3
{
/// <summary>
/// Classe que retorna a conexão com o banco de dados
/// </summary>
public class OracleConnectionFactory : IDbConnectionFactory
{
public DbConnection CreateConnection(string nameOrConnectionString)
{
return new OracleConnection(nameOrConnectionString);
}
}
/// <summary>
/// Classe mapeada para a tabela de Pessoas
/// </summary>
[Table("PESSOAS", Schema = "REPOSITORIO")]
public class Pessoa
{
[Key, DatabaseGenerated(DatabaseGeneratedOption.Identity), Column("ID")]
public decimal Id { get; set; }
[Column("NOME"), StringLength(50)]
public string Nome { get; set; }
public virtual ICollection<Telefone> Telefones { get; set; }
public Pessoa()
{
Telefones = new HashSet<Telefone>();
}
}
/// <summary>
/// Classe mapeada para a tabela de Telefones
/// </summary>
[Table("TELEFONES", Schema = "REPOSITORIO")]
public class Telefone
{
[Key, DatabaseGenerated(DatabaseGeneratedOption.Identity), Column("ID")]
public decimal Id { get; set; }
[Required, Column("PESSOA_ID")]
public decimal? PessoaId { get; set; }
[ForeignKey("PessoaId")]
public virtual Pessoa Pessoa { get; set; }
[StringLength(10), Required, Column("NUMERO")]
public string Numero { get; set; }
}
/// <summary>
/// Classe que representa o Contexto a ser utilizado para acesso ao banco
/// </summary>
public class MyDbContext : DbContext
{
private const string ConnectionString
= "DATA SOURCE=SERVER_ORACLE; User Id=REPOSITORIO; PASSWORD=repositorio; Persist Security Info=true";
static MyDbContext()
{
Database.DefaultConnectionFactory = new OracleConnectionFactory();
}
public MyDbContext()
: base(ConnectionString)
{
}
public DbSet<Pessoa> Pessoas { get; set; }
public DbSet<Telefone> Telefones { get; set; }
}
/// <summary>
/// Código para a aplicação Console
/// </summary>
class Program
{
static void Main(string[] args)
{
using (var db = new MyDbContext())
{
//adicionar uma pessoa
var pessoa = new Pessoa { Nome = "Jone Polvora" };
db.Pessoas.Add(pessoa);
db.SaveChanges();
//adicionar um telefone para a pessoa
var telefone = new Telefone { Numero = "6712345678", Pessoa = pessoa };
db.Telefones.Add(telefone);
db.SaveChanges();
//imprime a lista de pessoas
foreach (var p in db.Pessoas.ToList())
{
Console.WriteLine(p.Nome);
//imprime os telefones da pessoa atual
foreach (var t in p.Telefones)
{
Console.WriteLine(t.Numero);
}
}
//alterar a pessoa
pessoa.Nome = "Outro nome";
db.SaveChanges();
//remover o telefone da pessoa
db.Telefones.Remove(telefone);
db.SaveChanges();
//remover a pessoa
db.Pessoas.Remove(pessoa);
db.SaveChanges();
Console.WriteLine("Pressione Delete para remover todos os registros");
var key = Console.ReadKey();
if (key.Key == ConsoleKey.Delete)
{
db.Telefones.OrderBy(t => t.Id).ToList().ForEach(t => db.Telefones.Remove(t));
db.Pessoas.OrderBy(p => p.Id).ToList().ForEach(p => db.Pessoas.Remove(p));
db.SaveChanges();
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment